簡體   English   中英

如何將文件上傳到IIS中托管的.NET 3.5 WCF服務?

[英]How to upload a file to a .NET 3.5 WCF service hosted in IIS?

我已經扯了一段時間了。 有人可以提供一個非常簡單的示例(或指向工作示例的鏈接)來說明如何將文件上傳到IIS中托管的WCF服務。

我從簡單的事情開始。 我想通過POST從客戶端調用URL,傳遞文件名並發送文件。 因此,我在合同中添加了以下內容:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
void Upload(string fileName, Stream stream);

在.svc文件中實現了它:

public void Upload(string fileName, Stream stream)
{
    Debug.WriteLine((fileName));
}

立即,在運行項目時出現錯誤:

For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream.

不知道從這里去哪里。 希望看到一個實際的工作示例。

PS我在.NET 4中使用WCF 4進行了此操作,這看起來要簡單得多,但是我不得不降級。 在.NET 3.5中,我缺少了一些東西。

為了使其工作,您需要定義一個終結點,該終結點具有與WebHttpBinding兼容的綁定,並且該終結點已添加了WebHttpBehavior 該消息可能是紅色鯡魚,這是一個舊錯誤,如果您瀏覽到服務基址,並且啟用了元數據,則會顯示該錯誤。 另一個問題是,如果您希望能夠上載任何文件類型(包括JSON和XML),則需要定義一個WebContentTypeMapper來告訴WCF不要嘗試理解您的文件(有關更多信息,請訪問http:// blogs。 msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx )。

這是一個完整的例子。 3.5的最大問題是WebHttpBinding上不存在ContentTypeMapper屬性,因此您需要為此使用自定義綁定。 這段代碼使用自定義ServiceHostFactory定義了終結點,但是也可以使用config來定義。

Service.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyNamespace.MyService" Factory="MyNamespace.MyFactory" %>

Service.cs

using System;
using System.Diagnostics;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

public class MyNamespace
{
    [ServiceContract]
    public interface IUploader
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
        void Upload(string fileName, Stream stream);
    }

    public class Service : IUploader
    {
        public void Upload(string fileName, Stream stream)
        {
            Debug.WriteLine(fileName);
        }
    }

    public class MyFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new MyServiceHost(serviceType, baseAddresses);
        }

        class MyRawMapper : WebContentTypeMapper
        {
            public override WebContentFormat GetMessageFormatForContentType(string contentType)
            {
                return WebContentFormat.Raw;
            }
        }

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

            protected override void OnOpening()
            {
                base.OnOpening();

                CustomBinding binding = new CustomBinding(new WebHttpBinding());
                binding.Elements.Find<WebMessageEncodingBindingElement>().ContentTypeMapper = new MyRawMapper();
                ServiceEndpoint endpoint = this.AddServiceEndpoint(typeof(IUploader), binding, "");
                endpoint.Behaviors.Add(new WebHttpBehavior());
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM