【问题标题】:WCF Rest Webservice works on localhost but does not work on serverWCF Rest Webservice 在本地主机上工作,但在服务器上不起作用
【发布时间】:2014-09-24 11:11:58
【问题描述】:

我正在实现 WCF Rest WebGet 服务,它以 json 格式返回数据。它在 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 中尝试了不同的标签,但没有任何效果。

请提供帮助或提出任何想法。

如果需要任何其他信息,请告诉我。

【问题讨论】:

  • 您在端点中留下了一个空白地址,它正在寻找一个空地址吗?
  • 还有第二个,你的基地址设置在哪里?
  • 你能提供给我 web.config sn-p 吗?
  • 他们的 web.config 在第 4 点的长代码示例中
  • localhost:xxxxxxx" />

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


【解决方案1】:

您将地址指向 mex

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

所以试着把 mex 放在你的 url 上,比如:

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

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多