结合SOAP / JSON / XML在WCF中,使用UriTemplate [英] Combined SOAP/JSON/XML in WCF, using UriTemplate

查看:220
本文介绍了结合SOAP / JSON / XML在WCF中,使用UriTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立使用WCF,允许第三方开发人员挂钩到我们的软件的通用Web服务接口。经过一番挣扎和阅读(这个问题帮助了很多) ,我终于得到了SOAP,JSON和XML(POX)一起工作。

I'm trying to build a generic web service interface using WCF, to allow 3rd party developers to hook into our software. After much struggling and reading (this question helped a lot), I finally got SOAP, JSON and XML (POX) working together.

要简化,这是我的code(使这个例子简单,我没有使用接口 - 我曾尝试这种双向):

To simplify, here's my code (to make this example simple, I'm not using interfaces -- I did try this both ways):

<ServiceContract()> _
Public Class TestService
    Public Sub New()
    End Sub

    <OperationContract()> _
    <WebGet()> _
    Public Function GetDate() As DateTime
        Return Now
    End Function


    '<WebGet(UriTemplate:="getdateoffset/{numDays}")> _
    <OperationContract()> _
    Public Function GetDateOffset(ByVal numDays As Integer) As DateTime
        Return Now.AddDays(numDays)
    End Function

End Class

和在web.config code:

and the web.config code:

<services>
  <service name="TestService" 
           behaviorConfiguration="TestServiceBehavior">
    <endpoint address="soap" binding="basicHttpBinding" contract="TestService"/>
    <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="TestService"/>
    <endpoint address="xml" binding="webHttpBinding" behaviorConfiguration="poxBehavior" contract="TestService"/>
    <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="jsonBehavior">
      <enableWebScript/>
    </behavior>
    <behavior name="poxBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="TestServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

这实际工作 - 我能去 TestService.svc / XML / GETDATE 的XML, TestService.svc / JSON / GETDATE 的JSON,并在 TestService.svc?WSDL指向一个SOAP客户端并查询工作的SOAP。

This actually works -- I'm able to go to TestService.svc/xml/GetDate for xml, TestService.svc/json/GetDate for json, and point a SOAP client at TestService.svc?wsdl and have the SOAP queries work.

我想解决的部分是查询。我必须使用 TestService.svc / XML / GetDateOffset?numDays = 4 而不是 TestService.svc / XML / GetDateOffset / 4 。如果我指定UriTemplate,我得到的错误:

The part I'd like to fix is the queries. I have to use TestService.svc/xml/GetDateOffset?numDays=4 instead of TestService.svc/xml/GetDateOffset/4. If I specify the UriTemplate, I get the error:

使用UriTemplate端点不能与System.ServiceModel.Description.WebScriptEnablingBehavior使用。

不过,当然没有使用&LT; enableWebScript /&GT; ,JSON不起作用。

But of course without using <enableWebScript/>, JSON doesn't work.

我已经看到了,我想将工作正在3种不同的服务(.SVC文件),所有实现一个接口,它指定的合同,但在类指定的唯一的其他东西不同的WebGet / WebInvoke每个类属性。这似乎是一个很多额外的工作,也坦率地说,我不明白为什么该框架不处理我。类的实施将使用JSON VS时都相同,除了属性,这意味着随着时间的推移将是容易的错误/变化得到固定/在一个实施完成的而不是其他,导致不一致的行为SOAP实现的例子。

The only other thing I've seen that I think will work is making 3 different services (.svc files), that all implement an interface that specifies the contract, but in the classes specify different WebGet/WebInvoke attributes on each class. This seems like a lot of extra work, that frankly, I don't see why the framework doesn't handle for me. The implementation of the classes would all be the same, except for the attributes, which means over time it would be easy for bugs/changes to get fixed/done in one implementation but not the others, leading to inconsistent behaviour when using the JSON vs SOAP implementation for example.

我是不是做错了什么吗?我是不是采取了完全错误的做法和滥用WCF?有没有更好的办法做到这一点?

Am I doing something wrong here? Am I taking a totally wrong approach and misusing WCF? Is there a better way to do this?

通过我的经验做网站的东西,我觉得应该是可能的部分的一种框架来处理这个问题......我甚至都在我的脑海中如何构建它的想法。这似乎只是WCF是应该这样做的,我真的不希望重新发明轮子。

With my experience doing web stuff, I think it should be possible for some kind of framework to handle this ... I even have an idea in my head of how to build it. It just seems like WCF is supposed to be doing this, and I don't really want to reinvent the wheel.

推荐答案

实际上&LT; enableWebScript /&GT; 终止的不是的需要纯JSON支持。该<一href="http://msdn.microsoft.com/en-us/library/bb348930%28v=VS.90%29.aspx"><$c$c>WebScriptEnablingBehavior如果你想支持ASP.NET AJAX时,才需要。通常情况下,如果你想使用标准的脚本库的工作,你的的希望启用为您服务的这种支持。

Actually <enableWebScript /> is not required for "pure" JSON support. The WebScriptEnablingBehavior is only required if you want to support ASP.NET AJAX. More often than not, if you're trying to work with standard script libraries, you don't want to enable this support for your services.

相反,你要为你的JSON端点只需使用<一个什么href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.aspx"><$c$c>WebHttpBehavior并设置DefaultOutgoingResponseFormat =JSON。问题是,在.NET 3.5中,你无法控制通过配置此设置,因为<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.webhttpelement%28v=VS.90%29.aspx"><$c$c>WebHttpElement不公开这些属性进行配置。要解决此3.5已经提供了我称之为 EnhancedWebHttpElement <一实施href="http://stackoverflow.com/questions/1607755/problem-calling-wcf-service-library-from-jquery/1608336#1608336">here在这个答案另一个StackOverflow的问题。

Instead what you want to do for your JSON endpoint is just use the WebHttpBehavior and set the DefaultOutgoingResponseFormat="JSON". The problem is, in .NET 3.5, you cannot control this setting through config because WebHttpElement does not expose these properties for configuration. To work around this in 3.5 have supplied an implementation for what I call the EnhancedWebHttpElement here in this answer to another StackOverflow question.

幸运的是,微软意识到了这个缺点并通过<一所有 WebHttpBehavior 设置中启用配置href="http://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.webhttpelement_properties%28v=VS.100%29.aspx"><$c$c>WebHttpElement在4.0 。

Luckily Microsoft realized this shortcoming and enabled configuration of all the WebHttpBehavior settings via the WebHttpElement in 4.0.

这篇关于结合SOAP / JSON / XML在WCF中,使用UriTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆