我编写了一项服务,希望通过休息和 SOAP 来公开。我所读到的有关WCF 4.0的所有内容都表明,我只需要公开2个具有不同行为的端点即可执行此操作。但是我无法使它正常工作。

这是我的服务契约(Contract):

[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>

我正在使用路由定义我的服务网址:
public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService)));
        }
    }

我在这里做错什么了吗?我真的可以使用一些帮助。

最佳答案

我从未在配置中找到执行此操作的“正确”方法,但能够使用路由引擎来完成此操作。

我的全局asax文件现在如下所示:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("my/soap", new ServiceHostFactory(), typeof(MyService)));
            RouteTable.Routes.Add(new ServiceRoute("my/rest", new WebServiceHostFactory(), typeof(MyService)));
        }
    }

和我的配置是这样的:(启用其余的帮助页面)




<system.serviceModel>

    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint automaticFormatSelectionEnabled="true" helpEnabled="true"/>
        </webHttpEndpoint>
    </standardEndpoints>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

</system.serviceModel>

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

我喜欢它更符合asp.net MVC模型,并且几乎不需要任何配置。另外,通过这种方式可以使我从项目中完全删除.svc文件,这也是一个IMO。

10-04 17:51