WCF服务无法识别我自己的数据类型

时间:2010-09-06 20:31:18

标签: wcf

我正在尝试创建概念证明上传WCF服务。我在网上的博文中发现了以下内容:

namespace GreenWebMediaService
{    
    [ServiceContract]
    public interface IMediaServer
    {

        [OperationContract]
        void UploadData(UploadFile data);
    }


    // Use a data contract as illustrated in the sample below
    // to add composite types to service operations.
    [DataContract]
    public class UploadFile
    {
        public UploadFile() { }

        [MessageHeader]
        public string MetaData { get; set; }

        [MessageBodyMember]
        public Stream data { get; set; }
    }
}

我启动它(在Visual Studio中托管)并从新创建的Windows窗体应用程序中引用,这是我的参考,来自app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IMediaServer" closeTimeout="00:01:00"
                 openTimeout="00:01:00" receiveTimeout="00:10:00"
                 sendTimeout="00:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="65536" maxBufferPoolSize="524288"
                 maxReceivedMessageSize="65536" messageEncoding="Text"
                 textEncoding="utf-8" transferMode="Buffered"
                 useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192"
                        maxArrayLength="16384" maxBytesPerRead="4096"
                        maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                       realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:49689/MediaServer.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IMediaServer"
                contract="mediaServer.IMediaServer"
                name="BasicHttpBinding_IMediaServer" />
    </client>
  </system.serviceModel>
</configuration>

当我创建它的实例并像这样引用它时:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
            IMediaServer mediaServer = new mediaServer.MediaServerClient();
            UploadFile file = new UploadFile();            
            mediaServer.UploadData(null);
        }
    }

当我访问“file”变量时,它只有一个“file.ExtensionData”属性,而我本来希望可以访问我在“UploadFile”类中创建的属性。我错过了什么?我试图在visual studio中的配置对话框中打勾,因为我在其他线程中发现了这些信息,但仍然没有运气:请在此处查看我的对话框:http://screencast.com/t/NzhhNzM3Y2Q(对不起,我不知道screencast.com会保留多长时间图像)

2 个答案:

答案 0 :(得分:3)

您正在将DataContractMessageHeaderMessageBodyMember属性相结合。你应该使用

不是这些的组合。因此,您的UploadFile声明应如下所示:

[DataContract]
public class UploadFile
{
    [DataMember] public string MetaData { get; set; }
    [DataMember] public Stream data { get; set; }
}

或者像这样:

[MessageContract]
public class UploadFile
{
    [MessageHeader] public string MetaData { get; set; }
    [MessageBodyMember] public Stream data { get; set; }
}

答案 1 :(得分:1)

我在你的代码中看到两个问题。第一个问题是DataContract的定义。这是完全错误的。正如Ronald已经指出的那样,你正在组合DataContract和MessageContract。你必须使用罗纳德的第二个建议(首先不起作用):

[MessageContract] 
public class UploadFileRequest // Good naming convention for message contracts 
{ 
    // Streaming over HTTP demands only single body member of type Stream
    // any other data have to be passed as custom headers - that is the reason
    // why mentioned data contract doesn't work. 
    [MessageHeader] public string MetaData { get; set; } 
    [MessageBodyMember] public Stream Data { get; set; } 
} 

第二个问题是Stream作为您的请求的正文成员。只有在BasicHttpBinding上使用Streamed或StreamedRequest传输模式时才可以。必须手动在客户端绑定定义上设置此信息 - 它不会在WSDL /元数据中传输,也不是可互操作的功能。