开发者

Using WCF Web apis (REST) to support Streamed data

I have the following problem. Let me describe the steps I took so far...

  1. I created a new WCF Service Application in Visual Studio
  2. I then updated the project via Nuget to get the latest web http libs (webapi.dll)
  3. I then created a service method that looks like this

`

[ServiceContract]
public interface IService
{
        [OperationContract]
        [WebInvoke(Method="POST", UriTemplate="{value}")]
        string GetData(int value, Stream inputDocument);
}

`

Now attempting to view the my .svc in the browswer results in an error that says "For request in operation GetData to be a stream the operation must have a single parameter whose type is Stream"

I know this is an issue with configuration, I just don't know what needs to change in web.config Mind you, this seems to have been a common problem in WCF before the new HTTP support, I'm somewhat surprised that this doesn't work out of the box with the new APIs.

Any pointers?

Thanks

[EDIT] I've included my config...

<system.serviceModel>
    <services>
      <service name="MyService.Servi开发者_运维百科ce" behaviorConfiguration="serviceBehaviour">
        <endpoint behaviorConfiguration="endPointBehaviour" address="" binding="webHttpBinding" contract="MyService.IService"/>
      </service>
    </services>    
    <bindings>
      <webHttpBinding>
        <binding transferMode="Streamed" name="webHttpBinding" />
      </webHttpBinding>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="endPointBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="serviceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>    
  </system.serviceModel>


You are mixing up the new WCF Web API stuff with the old WCF REST stuff. Take a look at the HttpHelloResource sample as the simplest example of how to run a Web API service under IIS, or my blog post for an even simpler example of a service running in a console.

As for accepting a stream I think your simplest option would be an operation like this:

[ServiceContract]
public interface IService
{
        [OperationContract]
        [WebInvoke(Method="POST", UriTemplate="{value}")]
        string GetData(int value, HttpRequestMessage request);
}

and you can get the stream by doing

var stream = request.Content.ContentReadStream


Ok, so it seems the error message was taking me down the wrong path. I think that error message needs to be far more descriptive. Basically there's nothing wrong my code at all, it just doesn't make sense to point my browser to the .svc file as the service is not quite a WCF service. I learmt this by going ahead and accessing the service via code. And it works. Thanks for the help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜