Configuring XML-RPC behavior for IIS-hosted .SVC file?

孤街醉人 提交于 2019-11-29 07:46:12

Here are the steps to get this working:

  1. Download the XML-RPC for WCF sample
  2. The solution contains 3 projects: Microsoft.Samples.XmlRpc, TinyBlogEngine and TinyBlogEngineClient.
  3. Add a 4th project to the solution of type ASP.NET (I've called it TinyBlogEngineWeb)

In the new project reference Microsoft.Samples.XmlRpc and TinyBlogEngine.

Add a test.svc file to this web application which looks like this:

<%@ ServiceHost Language="C#" Debug="true" Service="TinyBlogEngine.BloggerAPI, TinyBlogEngine" %>

Add a XmlRpcEndpointBehaviorExtension class to the new project:

namespace TinyBlogEngineWeb
{
    public class XmlRpcEndpointBehaviorExtension : BehaviorExtensionElement
    {
        protected override object CreateBehavior()
        {
            // this comes from Microsoft.Samples.XmlRpc
            return new XmlRpcEndpointBehavior();
        }

        public override Type BehaviorType
        {
            get { return typeof(XmlRpcEndpointBehavior); }
        }
    }
}

Finally the system.serviceModel part of web.config should look like this:

<system.serviceModel>
  <services>
    <service name="TinyBlogEngine.BloggerAPI" behaviorConfiguration="returnFaults">
      <endpoint address="/blogger"
                binding="webHttpBinding"
                contract="TinyBlogEngine.IBloggerAPI"
                behaviorConfiguration="xmlRpcBehavior" />
    </service>
  </services>

  <behaviors>
    <serviceBehaviors>
      <behavior name="returnFaults">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>

    <endpointBehaviors>
      <behavior name="xmlRpcBehavior">
        <xmlRpc/>
      </behavior>
    </endpointBehaviors>
  </behaviors>

  <extensions>
    <behaviorExtensions>
      <add name="xmlRpc"
           type="TinyBlogEngineWeb.XmlRpcEndpointBehaviorExtension, TinyBlogEngineWeb" />
    </behaviorExtensions>
  </extensions>
</system.serviceModel>

Finally modify the console client application to use the address of the web project and test:

Uri blogAddress = new UriBuilder(
    Uri.UriSchemeHttp, 
    "localhost", 
    1260, // use the appropriate port here
    "/test.svc/blogger"
).Uri;

Tested with Windows Live Writer and the console client application. You can download my test solution from here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!