Search code examples
c#.netwcfiis

WCF service working on local not server IIS for functions


I have a basic WCF service sitting on a server IIS, a SQL db sits on it as well.

locally when I run it, it works fine and all my service functions work however once I publish to the server IIS I get

The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service.

Only when I call the /function

if I just go to the start page i get end point not found, which is the same on my local.

here is what my service.svc looks like

using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Web.Script.Services;
using Newtonsoft.Json.Linq;

namespace myNameSpace
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service
    {


        /// <summary>
        /// ....
        /// </summary>
        /// <returns>string</returns>
        [WebInvoke(UriTemplate = "/NextWeek", Method = "GET")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string nextWeek()
        {
            return db.Instance.getNextWeek();
        }

        //this continues for awhile and is basically the same type of functions

    }
}

I also created a global.asax

   public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service)));
        }
    }

and here is my web.config

I was originally getting a 404 error but i have managed to solve it to where it shows my service has no end point.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5" />
      </system.Web>
  -->
  <system.web>
    <compilation targetFramework="4.6" />
    <pages controlRenderingCompatibilityVersion="4.0" />
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default
            endpoint via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- 
              To avoid disclosing metadata information, set the value below to false and remove the
              metadata endpoint above before deployment 
          -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- 
              To receive exception details in faults for debugging purposes, set the value below to 
              true.  Set to false before deployment to avoid disclosing exception information 
          -->
          <serviceDebug includeExceptionDetailInFaults="true" />

        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
    .....
  </connectionStrings>
</configuration>

I have tried a few different options with the config I found on microsofts website but it hasn't helped.

thank you.


Solution

  • I noticed that you use the ORM framework to return DB entry. If the service works properly in local not server, it seems to me that there is something wrong with the database connection, try to check the connection string, avoid using windows integrated security to connect the database.
    Beside, here are my code snippets about hosting Restful style wcf service.
    Server(wcf service application)

    [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        Product GetProduct(int ID);
        public Product GetProduct(int ID)
        {
            TestStoreEntities entities = new TestStoreEntities();
            return entities.Products.FirstOrDefault(x => x.Id == ID);
        }
    

    Configuration.

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
          <add binding="webHttpBinding" scheme="http" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Connection string.

    <connectionStrings><add name="TestStoreEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=10.157.18.36;initial catalog=TestStore;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    

    Feel free to let me know if there is anything I can help with.