WCF 服务在本地而不是服务器 IIS 上工作以实现功能
WCF service working on local not server IIS for functions
我有一个基本的 WCF 服务位于服务器 IIS 上,SQL 数据库也位于其上。
在本地,当我 运行 它时,它工作正常并且我的所有服务功能都工作但是一旦我发布到服务器 IIS 我得到
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service.
仅当我调用/函数时
如果我只是转到起始页,我会得到找不到终点,这在我的本地是一样的。
这是我的 service.svc 的样子
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
}
}
我还创建了一个global.asax
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service)));
}
}
这是我的 web.config
我最初收到 404 错误,但我已设法将其解决到显示我的服务没有终点的位置。
<?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>
我已经尝试了几个不同的选项和我在微软网站上找到的配置,但没有帮助。
谢谢。
我注意到您使用 ORM 框架来 return 数据库条目。如果服务在本地而不是服务器正常工作,在我看来数据库连接有问题,尝试检查连接字符串,避免使用 windows 集成安全连接数据库。
此外,这是我关于托管 Restful 样式 wcf 服务的代码片段。
服务器(wcf服务应用)
[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);
}
配置。
<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>
连接字符串。
<connectionStrings><add name="TestStoreEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=10.157.18.36;initial catalog=TestStore;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
如果有什么我可以帮忙的,请随时告诉我。
我有一个基本的 WCF 服务位于服务器 IIS 上,SQL 数据库也位于其上。
在本地,当我 运行 它时,它工作正常并且我的所有服务功能都工作但是一旦我发布到服务器 IIS 我得到
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service.
仅当我调用/函数时
如果我只是转到起始页,我会得到找不到终点,这在我的本地是一样的。
这是我的 service.svc 的样子
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
}
}
我还创建了一个global.asax
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service)));
}
}
这是我的 web.config
我最初收到 404 错误,但我已设法将其解决到显示我的服务没有终点的位置。
<?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>
我已经尝试了几个不同的选项和我在微软网站上找到的配置,但没有帮助。
谢谢。
我注意到您使用 ORM 框架来 return 数据库条目。如果服务在本地而不是服务器正常工作,在我看来数据库连接有问题,尝试检查连接字符串,避免使用 windows 集成安全连接数据库。
此外,这是我关于托管 Restful 样式 wcf 服务的代码片段。
服务器(wcf服务应用)
[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);
}
配置。
<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>
连接字符串。
<connectionStrings><add name="TestStoreEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=10.157.18.36;initial catalog=TestStore;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
如果有什么我可以帮忙的,请随时告诉我。