我创建了一个非常简单的WCF,并通过“复制网站”将其部署到托管服务提供商。在本地,它设置为使用开发服务器(不在IIS中)。该域受完全信任权限。在本地,我可以访问Service.svc页面,但不能访问托管服务提供商。
代码如下:
Service.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" Factory="CustomHostFactory" %>
Service.cs
// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config and in the associated .svc file.
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
// If more than one base address exists then return the second address,
// otherwise return the first address
if (baseAddresses.Length > 1)
{
return new ServiceHost(serviceType, baseAddresses[1]);
}
else
{
return new ServiceHost(serviceType, baseAddresses[0]);
}
}
}
class CustomHost : ServiceHost
{
public CustomHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{ }
}
的Web.config
<system.serviceModel>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsHttpBinding" contract="IService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
任何帮助?
答案 0 :(得分:1)
一些事情:
您是否在托管服务提供商处创建了虚拟目录? * .svc文件必须部署到虚拟目录的根目录
您导航到http://yourserver/virtualdirectory/yourservice.svc
了吗?在IIS中托管时,所有基本地址都没有实际意义并且将被忽略 - 服务地址由Web服务器名称,虚拟目录和* .svc文件的名称定义
答案 1 :(得分:0)
我刚刚遇到了同样的问题。您使用的是Rob Zelt的文章WCF: This collection already contains an address with scheme http中的CustomHostFactory吗?
传入的baseAddresses
将包含为IIS设置的所有绑定;主机头,端口,http / https。传递给ServiceHost的绑定需要匹配WCF请求的URL - 否则您将无法找到“资源”。