C# WCF Rest Webservice在本地主机上工作,但在服务器上不工作
我正在实现WCF Rest WebGet服务,该服务以json格式返回数据。它可以在本地主机和本地IIS上完美工作 以下是我在localhost上调用webservice的方式:C# WCF Rest Webservice在本地主机上工作,但在服务器上不工作,c#,web-services,wcf,rest,web-config,C#,Web Services,Wcf,Rest,Web Config,我正在实现WCF Rest WebGet服务,该服务以json格式返回数据。它可以在本地主机和本地IIS上完美工作 以下是我在localhost上调用webservice的方式: http://localhost:59395/WallpaperService.svc/GetCategory?intId=1 但它在服务器上不起作用 以下两种情况在服务器上不起作用: (1) http://xyz.co.in/WallpaperService.svc 它给出了以下错误: "Endpoint not
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>
代码片段:
1.IWallpaperServices.cs
使用系统文本;
名称空间令人惊叹的壁纸
{
//注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码和配置文件中的接口名称“IAndroidAppWCF”。
[服务合同]
公共接口IWallpaperService
{
[经营合同]
[WebGet(UriTemplate=“GetCategory?intId={intId}”,ResponseFormat=WebMessageFormat.Json)]
字符串GetCategory(int intId);
}
}
2.壁纸服务.svc
3.WallperService.svc.cs
使用令人敬畏的壁纸。控制器;
使用令人惊叹的壁纸;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用系统文本;
使用System.Web.Script.Serialization;
名称空间令人惊叹的壁纸
{
公共类壁纸服务:IWallpaperService
{
公共字符串GetCategory(int intId)
{
尝试
{
List lstCategory=新列表();
DataController objController=新DataController();
lstcegory=objController.GetAllCategory(intId);
if(lstcegory!=null&&lstcegory.Count>0)
{
JavaScriptSerializer js=新的JavaScriptSerializer();
返回js.Serialize(lstcontegory);
}
}
捕获(例外情况除外)
{
ErrorLog.Write(ex);
}
返回字符串。空;
}
}
}
4.Web.config
我在Web.config中尝试了不同的标记,但没有任何效果
请提供帮助或建议
如果需要任何其他信息,请告诉我。您将地址指向mex
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
您在端点中留下了一个空白地址,它是在寻找一个空地址吗?第二个地址也是,您的基址设置在哪里?您能提供web.config代码段吗?他们的web.config在第4点的长代码示例中“/>
http://xyz.co.in/WallpaperService.svc/mex