【问题标题】:WCF Rest Webservice with stream带有流的 WCF Rest Web 服务
【发布时间】:2011-06-16 02:26:18
【问题描述】:

我感兴趣地阅读了以下帖子,因为它完全复制了我遇到的问题(并且让我发疯) “对于将 UploadFile 操作中的请求作为流,该操作必须有一个类型为 Stream 的参数。” -http://social.msdn.microsoft.com/Forums/en/wcf/thread/80cd26eb-b7a6-4db6-9e6e-ba65b3095267

我几乎遵循了我找到的所有代码/示例,但仍然无法解决这个错误 - http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx

我想要实现的只是使用标准文件名/流参数从 android 设备发布图像(jpeg/png)。很可能这是我配置错误、误解或遗漏的简单事情,但我需要有一个概念证明的解决方案。

 public interface IConXServer
    {
    [OperationContract]
    [WebInvoke(UriTemplate = "UploadImage({fileName})", Method="POST")]
    void UploadImage(string fileName, Stream imageStream);
    }

 public class ConXWCFServer : IConXServer
    {
    public void UploadImage(string fileName, Stream imageStream)
       {
       //implement image save
       }
    }

web.config 设置 -->

<standardEndpoints>
   <webHttpEndpoint>
       <standardEndpoint name="webHttpEndpoint" helpEnabled="false"/>
   </webHttpEndpoint>
</standardEndpoints>

<bindings>
    <webHttpBinding>
        <binding name="webHttpBinding" transferMode="Streamed"/>
    </webHttpBinding>
</bindings>

<behaviors>
    <endpointBehaviors>
        <behavior name="webHttpBehavior">
            <webHttp/>
        </behavior>
    </endpointBehaviors> 
    <serviceBehaviors>
        <behavior>
            <serviceMetadata httpGetEnabled="false"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
            <serviceThrottling maxConcurrentCalls="2147483647"  maxConcurrentSessions="2147483647"/>
        </behavior>
    </serviceBehaviors>
</behaviors>

使用 vs2010 和 IIS Express。如果我注释掉上述方法,所有其他方法都可以工作并返回数据以及 wsdl 查询

提前致谢 紧缩

【问题讨论】:

  • 对于将UploadFile 操作中的请求作为流,该操作必须有一个类型为Stream 的参数。浏览元数据端点时发生错误。
  • (5 年后..)这个要了我的命。我有两个 WCF 服务项目,都是 .Net 4.5.1。一方面,以 Stream 作为 3 个参数中的 1 个的服务可以完美运行,另一方面,它会引发此异常。我对这两个 web.config 进行了文件比较,它们是相同的......但我无法解决这个问题。即使重新创建“有故障的”服务也无法修复它。我很困惑。

标签: wcf rest stream


【解决方案1】:

您提到了 WSDL,这让我相信您在尝试浏览服务的元数据端点时遇到了错误。因此,首先,WSDL 和 REST 不能一起使用,因此您根本不应该期望将它用于 REST 接口。忘记服务元数据概念甚至存在于 REST 世界中。

Next 虽然 REST 的 webHttpBinding 确实支持 Stream body 参数前面的参数,但其他绑定不支持,并且必须有单个 Stream 参数或带有 headers 和 stream body 的消息协定。

所以,最后,问题根本不在于 REST webHttpBinding,我敢打赌它工作得很好。如果不是这样,我会非常震惊,因为你没有做任何不应该在那个部门工作的事情。问题是您希望元数据端点为您定义的服务合同生成 WSDL,而这不受支持。

【讨论】:

  • 感谢您的洞察力。让我看看我是否理解(我的 WCF 是有限的)实际上你是说 REST 端点将接受我在尝试中描述的参数/流,并且错误实际上是由元数据端点生成的——我无意中忘记了它是一个单独的端点,因此是单独的绑定机制 - 仅支持单个流参数?
  • 正确,WSDL 生成器实现是为 SOAP/RPC 服务合同而设计的,在那些您不能拥有各种内在或数据合同类型、流或消息合同的单独参数的服务合同上。你不能把这三个混在一起。因此,当它尝试为您的 REST 服务合同生成元数据时,在混合中将 Stream 参数作为正文的最后一个参数是完全有效的,它会因您看到的错误而爆炸,因为它从不希望遇到这样的签名。
  • 好的。这些休息服务是为移动 html5/css/js 概念验证而设置的——它已被标记为支持原生编码。 REST 甚至适用于移动设备上的这种场景,还是我们应该考虑切换回 basicHttp?我们希望有 WSDL 和在没有 SOAP 的情况下接收 XML (POX) 的能力,但我猜这是冲突?
  • 如果您想使用 WSDL/RPC,那么您就不想使用 REST……这两者是相互冲突的理想。 :) 任何一个都可以正常工作,但是根据您的移动开发平台,您可能会更难使用 SOAP。如果您只是围绕 POX 发送并且不需要 SOAP 的好处(即与 WS-* 标准互操作),那么您应该使用带有 XML 编码的 webHttpBinding 并完成它。 REST 不仅仅是 HTTP+XML ,它是一种设计 Web API 的不同方式。如果 RPC 风格适合您的需求,您不一定要陷入所有这些。
  • 嗨,我在 Internet Explorer 上打开 localhost:6842/ImageService.svc 时遇到了同样的错误。我有这个问题是因为我试图在 Internet Explorer 上查看 WSDL 吗?换句话说,这与您所说的错误相同吗?我问过这个问题:stackoverflow.com/questions/13601238/…
【解决方案2】:

我是这样做的,而且效果很好。

将工厂类添加到 web 服务 (WcfService2.ServiceFactory)

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService2.Service1" CodeBehind="Service1.svc.cs" Factory="WcfService2.ServiceFactory" %>

我的界面:

public interface IService1

 {

[OperationContract]
        [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, Method = "POST",
         RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "UploadFile/{fileName}")]

        void UploadFile(string fileName, Stream fileContent);


 }

我的方法:

public void UploadFile(string fileName, Stream fileContent)
        {

            var pathfile = "\\\\SERVER\\TravelsRequestFiles";
            using (var fileStream = new FileStream(string.Concat(pathfile, "\\", fileName), FileMode.Create, FileAccess.Write))
            {
                fileContent.CopyTo(fileStream);
            }

        }

我的工厂类:

public class ServiceFactory : ServiceHostFactory

    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)

        {
            return new MyServiceHost(serviceType, baseAddresses);
        }

        class MyServiceHost : ServiceHost

        {
            public MyServiceHost(Type serviceType, Uri[] baseAddresses)
              : base(serviceType, baseAddresses)
            {
            }

            protected override void InitializeRuntime()
            {
                ServiceEndpoint endpoint = this.Description.Endpoints[0];
                endpoint.Behaviors.Add(new EndpointBehaviors());

                base.InitializeRuntime();
            }
        }
    }

我添加了一个 EndpointBehaviors 类,它找到了操作 UploadFile 并删除了它的 DataContractSerializerOperationBehavior,然后就可以工作了!

public class EndpointBehaviors: IEndpointBehavior

    {

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {

            ContractDescription cd = endpoint.Contract;
            foreach (DispatchOperation objDispatchOperation in endpointDispatcher.DispatchRuntime.Operations)
            {
                if (objDispatchOperation.Name.Equals("UploadFile"))
                {
                    OperationDescription myOperationDescription = cd.Operations.Find("UploadFile");

                    DataContractSerializerOperationBehavior serializerBehavior = myOperationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
                    myOperationDescription.Behaviors.Remove(serializerBehavior);                
                }
            }
        }
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            BindingElementCollection elements = endpoint.Binding.CreateBindingElements();
            WebMessageEncodingBindingElement webEncoder = elements.Find<WebMessageEncodingBindingElement>();
            if (webEncoder == null)
            {
                throw new InvalidOperationException("This behavior must be used in an endpoint with the WebHttpBinding (or a custom binding with the WebMessageEncodingBindingElement).");
            }           
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多