简体   繁体   English

WCF Rest Web服务在localhost上运行,但在服务器上不运行

[英]WCF Rest Webservice works on localhost but does not work on server

I am implementing WCF Rest WebGet service which returns data in json format. 我正在实现WCF Rest WebGet服务,该服务以json格式返回数据。 It perfectly works on localhost and local IIS. 它可以完美地在localhost和本地IIS上运行。

This is how I call webservice on localhost: 这是我在本地主机上调用webservice的方式:

http://localhost:59395/WallpaperService.svc/GetCategory?intId=1

But it does not work on server. 但是它在服务器上不起作用。

Below two scenarios on server does not work: 服务器上的以下两种情况不起作用:

(1) http://xyz.co.in/WallpaperService.svc (1) http://xyz.co.in/WallpaperService.svc

It gives below error: 它给出以下错误:

"Endpoint not found. Please see the service help page for constructing valid requests to the service."

(2) http://xyz.co.in/WallpaperService.svc/GetCategory?intId=1 (2) http://xyz.co.in/WallpaperService.svc/GetCategory?intId=1

It gives below error: HTTP 404 Resource not found 它给出以下错误:找不到HTTP 404资源

Code Snippet:

1. IWallpaperServices.cs


    using System.Text;

    namespace AwesomeWallpapers
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IAndroidAppWCF" in both code and config file together.
        [ServiceContract]
        public interface IWallpaperService
        {
            [OperationContract]
            [WebGet(UriTemplate = "GetCategory?intId={intId}",ResponseFormat=WebMessageFormat.Json)]
            string GetCategory(int intId);
        }
    }


2. WallpaperService.svc

     <%@ ServiceHost Language="C#" 
                        Debug="true" 
                        Service="AwesomeWallpapers.WallpaperService" 
                        CodeBehind="WallpaperService.svc.cs" 
                        Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

3. WallpaperService.svc.cs

using AwesomeWallpapers.Controllers;
using AwesomeWallpapers.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Script.Serialization;

namespace AwesomeWallpapers
{    
    public class WallpaperService : IWallpaperService
    {
        public string GetCategory(int intId)
        {
            try
            {
                List<Category_GetAllResult> lstCategory = new List<Category_GetAllResult>();
                DataController objController = new DataController();
                lstCategory = objController.GetAllCategory(intId);
                if (lstCategory != null && lstCategory.Count > 0)
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    return js.Serialize(lstCategory);
                }
            }
            catch (Exception ex)
            {
                ErrorLog.Write(ex);
            }
            return string.Empty;
        }
    }
}

4. Web.config

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=152368
      -->

    <configuration>
      <connectionStrings>
        <add name="TestDBConnectionString" connectionString="Data Source=x.y.z.a;Initial Catalog=WallsNRings;User ID=abc;Password=abc"
          providerName="System.Data.SqlClient" />
      </connectionStrings>
      <appSettings>
        <add key="webpages:Version" value="1.0.0.0"/>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
      </appSettings>

      <system.web>
        <customErrors mode="Off"/>
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          </assemblies>
        </compilation>

        <authentication mode="Forms">
          <forms loginUrl="~/Account/LogOn" timeout="2880" />
        </authentication>

        <pages>
          <namespaces>
            <add namespace="System.Web.Helpers" />
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="System.Web.WebPages"/>
          </namespaces>
        </pages>
      </system.web>

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

      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>

      <system.serviceModel>
        <services>
          <service behaviorConfiguration="Default" name="AwesomeWallpapers.WallpaperService">
            <endpoint address="" 
                      behaviorConfiguration="webBehavior" 
                      binding="webHttpBinding" 
                      contract="AwesomeWallpapers.IWallpaperService" />
            <endpoint contract="IMetadataExchange" 
                      binding="mexHttpBinding" 
                      address="mex" />
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webBehavior">
              <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="Default">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    </configuration>

I tried different tags in Web.config but nothing works. 我在Web.config中尝试了其他标签,但没有任何效果。

Please help or suggest any ideas. 请帮助或提出任何想法。

Let me know if any additional information is required. 让我知道是否需要其他信息。

You're pointing the address to mex 您将地址指向mex

<endpoint contract="IMetadataExchange" 
                      binding="mexHttpBinding" 
                      address="mex" />

So try put mex at the of your url, like: 因此,请尝试将mex放在您网址的,例如:

http://xyz.co.in/WallpaperService.svc/mex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM