收录日期:2019/06/20 13:43:56 时间:2010-10-04 18:29:42 标签:asp.net-mvc,wcf,xml-rpc

I'm using Clemens Vasters' XML-RPC implementation to implement an XML-RPC endpoint. When I host the service in a console application, it works fine.

I'd like to host it in an ASP.NET MVC application, so I'm using a .SVC file. This is partially working. When I browse to the .SVC file, I see the usual "You have created a service" stuff.

However, when I point my XML-RPC client (Windows Live Writer) at the same location, I get a "400 Bad Request". I'm guessing that this is because my service isn't correctly exposed as XML-RPC.

I've attempted to configure an endpoint behavior in Web.config, as follows:

<system.serviceModel>
  <services>
    <service name="AnotherBlogEngine.Web.Api.BlogApi">
      <endpoint address=""
                binding="webHttpBinding"
                contract="AnotherBlogEngine.Web.Api.IBlogApi"
                behaviorConfiguration="xmlRpcBehavior" />
  </service>
  </services>
  <extensions>
    <behaviorExtensions>
      <add name="xmlRpc"
           type="AnotherBlogEngine.XmlRpc.XmlRpcEndpointBehaviorElement, \
                 AnotherBlogEngine.XmlRpc" />
    </behaviorExtensions>
  </extensions>
  <behaviors>
    <endpointBehaviors>
      <behavior name="xmlRpcBehavior">
        <xmlRpc/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

...but it's not working. What am I doing wrong? Do I have my Web.config correct, or do I have it all completely wrong? Do I need to provide a custom factory in my .SVC file to sort out the behaviors?

The .SVC file looks like this, by the way:

<%@ ServiceHost Language="C#" Debug="true"
    Service="AnotherBlogEngine.Publishing.Service.BlogApi, \
             AnotherBlogEngine.Publishing.Service" %>

Note: Those backslashes aren't actually there; they're just for line-wrapping.

Also, at the moment, I'm just testing this in Cassini (VS2010), rather than IIS, but I'll be aiming it at IIS 7.x.

Update: It's definitely at least looking at my extension: it calls XmlRpcEndpointBehaviorElement.get_BehaviorType, and if XmlRpcEndpointBehavior doesn't implement IEndpointBehavior, I get an error to that effect (displayed in place of the "You have created a service" page).

However, breakpoints on the other methods in either class don't get hit.

If I turn on WCF tracing, I see "unrecognized message version" in the log file.

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.