0
votes

I have a restful .svc-less,fileless service which returns JSon data. It works fine if it run it locally but when i host in in my local IIS7, i get error

Request Error The server encountered an error processing the request. See server logs for more details.

This is the path of the web host application in IIS.

localhost:8080/AdventureWorksHost/EmployeeService/Employees/GetEmployees

But works fine when I run it in my visual studio

localhost:50182/employeeService/Employees/Getemployees

where AdventureWorksHost is ApplicationName hosted under Default Web Site in IIS. And EmployeeService is name of the Service which I service I added in global.asax.cs with standard endpoints in webconfig file

            RouteTable.Routes.Add(new ServiceRoute("EmployeeService", new WebServiceHostFactory(), typeof(Services.EmployeeService.EmployeeService)));

I have checked several suggestion in stackoverflow and other sites for similar issue but none of solution works for me.

1)I assigned default port number 8080 for Default Web Site(under which my project is hosted) in IIS7 and made sure this port number is not blocked by firewall or any anti-virus application in my computer. 2)I check WCF HTTP and NON-HTTP activations, WCF Services for all .Net Framework avaiable in Windows Features

Windows features

3)I have given added permission for IIS_IUSRS to the WebConfig of my application

4)I ran this in cmd prompt: aspnet_regiis.exe -iru C:\Windows\Microsoft.NET\Framework\v4.0.30319

5)I ran ServiceModelReg.exe -i from the “%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation” directory to register the script maps

4) and 5) I used forums.asp.net/t/1807824.aspx?WCF+Service+works+locally+but+returns+404+on+remote+server

6)Added below as per stackoverflow.com/questions/4793127/404-when-running-net-4-wcf-service-on-iis-no-svc-file

        <system.webServer>
            <validation validateIntegratedModeConfiguration="false"/>
            <modules runAllManagedModulesForAllRequests="true"/>
          </system.webServer>

I've tried every solution suggested on internet. Nothing works on my local IIS7

            <?xml version="1.0" encoding="utf-8"?>
        <!--
          For more information on how to configure your ASP.NET application, please visit
          http://go.microsoft.com/fwlink/?LinkId=169433
          -->
        <configuration>
           <configSections>
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
          </configSections>

          <system.diagnostics>  
              <sources>  
                    <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">  
                    <listeners>  
                       <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Users\Julian Luwang\Documents\Visual Studio 2013\Projects\AdventureWorksEntityFramework\AdventureWorksHost\Log\Traces.svclog" />  
                    </listeners>  
                 </source>  
              </sources>  
           </system.diagnostics>  

          <system.web>

            <httpRuntime executionTimeout="1800000000" />
            <compilation debug="true" targetFramework="4.5" />
                <customErrors mode="On">
                </customErrors>
          </system.web>
          <system.serviceModel>
               <standardEndpoints>
              <webHttpEndpoint>
                  <standardEndpoint name="test" helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
                  <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" />
                      <security mode="None"></security>
               </standardEndpoint>
                </webHttpEndpoint>
            </standardEndpoints>
           <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
        </serviceHostingEnvironment> 
           <behaviors>
              <serviceBehaviors>
                <!--<behavior name="Secured">
                  <serviceMetadata httpsGetEnabled="true" />
                  <serviceDebug includeExceptionDetailInFaults="true" />
                  <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                </behavior>-->
                <behavior>
                <serviceDebug httpHelpPageEnabled="true" httpsHelpPageEnabled="false" />
            </behavior>

                <behavior name="Normal">
                  <serviceMetadata httpGetEnabled="true" />
                  <serviceDebug includeExceptionDetailInFaults="true" />
                  <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                </behavior>
                     </serviceBehaviors>
            </behaviors>
            <bindings>
                 <webHttpBinding>
                 <!-- Binding (non-secured) -->
                <binding name="Normal" transferMode="Streamed" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
                  <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" />
                  <security mode="None" />
                </binding>
              </webHttpBinding>
           </bindings>

             <!--protocol mapping added-->

               <protocolMapping>
              <add scheme="http" binding="webHttpBinding" bindingConfiguration="Normal" />

            </protocolMapping>

              </system.serviceModel>
          <connectionStrings>
            <add name="AdventureWorks2012Entities" connectionString="metadata=res://*/AdventureWorksEDM.csdl|res://*/AdventureWorksEDM.ssdl|res://*/AdventureWorksEDM.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=JULIAN\SQLEXPRESS;initial catalog=AdventureWorks2012;integrated security=True;trusted_connection=yes;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
          </connectionStrings>
          <entityFramework>
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
            <providers>
              <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            </providers>
          </entityFramework>
          <system.webServer>
            <validation validateIntegratedModeConfiguration="false" />
                <modules runAllManagedModulesForAllRequests="true">

                <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
             </modules> 
           <handlers>
            <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" />

            </handlers>
                 <directoryBrowse enabled="true" />
                <defaultDocument enabled="false">
                    <files>
                        <add value="EmployeeService" />
                    </files>
                </defaultDocument>
          </system.webServer>
        </configuration>

The Service

            namespace AdventureWorksHost.Services.EmployeeService
        {

            [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
            public class EmployeeService : IEmployeeService
            {

                [WebGet(UriTemplate = "Employees/GetEmployees", ResponseFormat = WebMessageFormat.Json)]
                public List<EmployeeItem> GetEmployee()
                {
                      try
                    {
                        List<EmployeeItem> tReturn = new List<EmployeeItem>();
                       using (AdventureWorks2012Entities tClienEntities = new AdventureWorks2012Entities())
                        {
                             var tEmployees = (from es in tClienEntities.Employees
                                             orderby es.JobTitle
                                              select new
                                              {
                                                  es.rowguid,
                                                  es.BirthDate,
                                                  es.BusinessEntityID,
                                                  es.JobTitle,
                                                  es.NationalIDNumber,
                                                  es.MaritalStatus,
                                                  es.LoginID,
                                              }).ToList();


                            foreach (var tEmployee in tEmployees)
                            {
                                EmployeeItem tEmployeeItem = new EmployeeItem();
                                tEmployeeItem.rowguid = tEmployee.rowguid;
                                tEmployeeItem.BusinessEntityId = tEmployee.BusinessEntityID;
                                tEmployeeItem.JobTitle = tEmployee.JobTitle;
                                tEmployeeItem.MaritalStatus = tEmployee.MaritalStatus.ToString();
                                tEmployeeItem.NationalIDNumber = Int32.Parse(tEmployee.NationalIDNumber.ToString());
                                tEmployeeItem.BirthDate = tEmployee.BirthDate;
                                tEmployeeItem.LoginID = tEmployee.LoginID.ToString();
                                List<Person> Persons = new List<Person>();

                                foreach (var tPersons in (from esp in tClienEntities.People
                                                          where esp.BusinessEntityID == tEmployeeItem.BusinessEntityId
                                                          orderby esp.FirstName
                                                          select new
                                                          {
                                                              esp.rowguid,
                                                              esp.FirstName,
                                                              esp.MiddleName,
                                                              esp.LastName,
                                                              esp.PersonType,
                                                              esp.Demographics

                                                          }))
                                {

                                   Persons.Add(new Person()
                                    { 
                                        rowguid = tPersons.rowguid,
                                        FirstName = tPersons.FirstName,
                                        MiddleName = tPersons.MiddleName,
                                        LastName = tPersons.LastName,
                                        PersonType = tPersons.PersonType,
                                        Demographics = tPersons.Demographics
                                    });
                                } 
                                tEmployeeItem.Persons = Persons;
                                tReturn.Add(tEmployeeItem);
                            }
                        }

                        return tReturn;

                    } 

                    catch (FaultException ex)
                    {
                        throw (ex);
                    }

                } 
            }

        }

My application AdventureWorkHost look like this in my local IIS7

Application in IIS7

1
Is it because of database is in local SQL Server? I read somewhere that if my root account of my IIS7 as the same administrator account I used for my sql server(without a password), then there shouldn't be any database issue.dan joe

1 Answers

0
votes

Resolved: I checked the error in server logs of SQL server management studio. The error was

Message Login failed for user 'NT AUTHORITY\NETWORK SERVICE'. Reason: Failed to open the explicitly specified database 'AdventureWorks2012'. [CLIENT: ]

So, I tried a few things.

1) Added NT AUTHORITY\NETWORK SERVICE in Security access to folder C:\inetpub\wwwroot

2)in local IIS, change identity of the application pool used by the hosted application to network service from applicationPoolIdentity

3)add new login NT AUTHORITY\NETWORK SERVICE in security of the SQL MGMT STUDIO, specify properties of the new login. 4)Set default database to your database 5)Select databases and role membership for new login as db_datareader,db_datawriter and db_owner and public 6)Set server role public and sysadmin 7)Select database->Security->User(the newly created login). Open it 8)Set owned schema as db_datareader,db_datawriter and db_owner 9)Set role membership as db_datareader,db_datawriter and db_owner

Restart local sql server and then run.