【发布时间】:2025-12-31 02:35:06
【问题描述】:
我编写了一个服务,我希望通过 rest 和 soap 来公开它。我读到的关于 WCF 4.0 的所有内容都表明我只需要公开 2 个具有不同行为的端点来执行此操作。但我无法让它工作。
这是我的服务合同:
[ServiceContract]
public interface MyService
{
[OperationContract]
[WebGet(UriTemplate="data/{value}")]
string GetData(string value);
}
这是我的 web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyService">
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
<endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="MyService" />
<endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" contract="MyService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp automaticFormatSelectionEnabled="true" helpEnabled="true" />
</behavior>
<behavior name="soapBehavior" />
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
我正在使用路由来定义我的服务 url:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService)));
}
}
我在这里做错了什么吗?我真的需要一些帮助。
【问题讨论】:
-
我还发现这个其他 question/answer 很有帮助。