同一端点上的两个休息式 WCF 合同(svc 文件)
Two rest-style WCF contracts on same endpoint(svc file)
我试图在同一个 SVC 文件下创建两个合同,但它不起作用。当我在 IE 中打开 SVC 文件时,出现 'Endpoint error' 错误。
如果我从客户端调用合同的方法,它不会访问该服务。
服务器托管在 IIS 上。
请让我知道这里发生了什么。
另外,是否可以在同一个 SVC 文件或 Web 服务下使用一个 wsHttpBinding 和一个 webHttpBinding?它们将托管在 IIS 上。
SVC文件
<%@ ServiceHost Language="C#" Debug="true" Service="S1.ServiceDual6" CodeBehind="ServiceDual6.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
合同
{
[InspectorBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceDual6 : IServiceREST, IServiceREST2
{
public string RestInDual_Contract(Message mm)
{
return "";
}
public string RestInDual_Contract2(Message mm)
{
return "";
}
}
}
namespace S1
{
[ServiceContract]
public interface IServiceREST
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string RestInDual_Contract(Message mm);
}
[ServiceContract]
public interface IServiceREST2
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string RestInDual_Contract2(Message mm);
}
}
Web.config
<service name="S1.ServiceDual6" behaviorConfiguration="s1bhrWithHttpEnabled">
<host>
<baseAddresses>
<add baseAddress="http://localhost/S2/Service6.svc"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="S1.IServiceREST" behaviorConfiguration="s1bhr">
</endpoint>
<endpoint address="" binding="webHttpBinding" contract="S1.IServiceREST2" behaviorConfiguration="s1bhr">
</endpoint>
</service>
如果托管在单个端点上,您需要通过提供不同的路径来区分这两种服务。通过在 address
字段中添加唯一值来分隔这两者。
<endpoint address="Rest/v1" binding="webHttpBinding" contract="S1.IServiceREST" behaviorConfiguration="s1bhr">
</endpoint>
<endpoint address="Rest/v2" binding="webHttpBinding" contract="S1.IServiceREST2" behaviorConfiguration="s1bhr">
</endpoint>
现在您可以通过在基地址中为 "S1.IServiceREST" 附加 Rest/V1
和为 "S1.IServiceREST2" 附加 Rest/V2
来调用它们。
我试图在同一个 SVC 文件下创建两个合同,但它不起作用。当我在 IE 中打开 SVC 文件时,出现 'Endpoint error' 错误。 如果我从客户端调用合同的方法,它不会访问该服务。 服务器托管在 IIS 上。
请让我知道这里发生了什么。
另外,是否可以在同一个 SVC 文件或 Web 服务下使用一个 wsHttpBinding 和一个 webHttpBinding?它们将托管在 IIS 上。
SVC文件
<%@ ServiceHost Language="C#" Debug="true" Service="S1.ServiceDual6" CodeBehind="ServiceDual6.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
合同
{
[InspectorBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceDual6 : IServiceREST, IServiceREST2
{
public string RestInDual_Contract(Message mm)
{
return "";
}
public string RestInDual_Contract2(Message mm)
{
return "";
}
}
}
namespace S1
{
[ServiceContract]
public interface IServiceREST
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string RestInDual_Contract(Message mm);
}
[ServiceContract]
public interface IServiceREST2
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string RestInDual_Contract2(Message mm);
}
}
Web.config
<service name="S1.ServiceDual6" behaviorConfiguration="s1bhrWithHttpEnabled">
<host>
<baseAddresses>
<add baseAddress="http://localhost/S2/Service6.svc"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="S1.IServiceREST" behaviorConfiguration="s1bhr">
</endpoint>
<endpoint address="" binding="webHttpBinding" contract="S1.IServiceREST2" behaviorConfiguration="s1bhr">
</endpoint>
</service>
如果托管在单个端点上,您需要通过提供不同的路径来区分这两种服务。通过在 address
字段中添加唯一值来分隔这两者。
<endpoint address="Rest/v1" binding="webHttpBinding" contract="S1.IServiceREST" behaviorConfiguration="s1bhr">
</endpoint>
<endpoint address="Rest/v2" binding="webHttpBinding" contract="S1.IServiceREST2" behaviorConfiguration="s1bhr">
</endpoint>
现在您可以通过在基地址中为 "S1.IServiceREST" 附加 Rest/V1
和为 "S1.IServiceREST2" 附加 Rest/V2
来调用它们。