WCF Rest Webservice可在localhost上运行,但在服务器上不起作用

时间:2014-09-24 11:11:59

标签: c# web-services wcf rest web-config

我正在实现以json格式返回数据的WCF Rest WebGet服务。它完全适用于localhost和本地IIS。

这就是我在localhost上调用webservice的方式:

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

但它在服务器上不起作用。

服务器上的两个场景下面不起作用:

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

它给出了以下错误:

"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

它给出了以下错误: 找不到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>

我在Web.config中尝试了不同的标签,但没有任何效果。

请帮助或提出任何想法。

如果需要任何其他信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

您将地址指向mex

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

因此,请尝试将mex放在您的网址上,例如:

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