代码之家  ›  专栏  ›  技术社区  ›  Leonid

WCF将图像上载到服务器绑定错误

  •  0
  • Leonid  · 技术社区  · 15 年前

    我对WFC服务器或客户端配置有问题。 当我尝试将图像上载到服务器时,出现错误:

    Content Type multipart/related; type="application/xop+xml";
    start="<http://tempuri.org/0>";
    boundary="uuid:868d52b2-10f6-4661-8784-bf81f3a84c16+id=1";
    start-info="application/soap+xml" was not supported by service http://localhost:8080/PhotoService/.  The client and service bindings may be mismatched.
    

    有人能帮我配置服务器和客户端,或者告诉我哪里出错了吗?

    我有这样的配置:

    在客户端:

    <configuration>
        <system.serviceModel>
            <bindings>
                <wsHttpBinding>
                    <binding name="WSHttpBinding_IPhotoService" maxReceivedMessageSize ="2000000" messageEncoding="Mtom" maxBufferPoolSize="2000000">
                    <readerQuotas maxArrayLength="50000000" maxStringContentLength="2000000"/>
                      <security mode="None">
                        <transport>
                          <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                      </security>
                    </binding>
                </wsHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost:8080/PhotoService/"
                    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPhotoService"
                    contract="PhotoService.IPhotoService" name="WSHttpBinding_IPhotoService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
            </client>
        </system.serviceModel>
    </configuration>
    

    在服务器端:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
        <services>
          <service name="PhotoService.PhotoService">
            <endpoint address="" binding="wsHttpBinding" contract="PhotoService.IPhotoService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/PhotoService/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
          <wsHttpBinding>
            <binding name="wsHttp" maxReceivedMessageSize ="2000000" messageEncoding="Mtom" maxBufferPoolSize="2000000">
              <readerQuotas maxArrayLength="50000000" maxStringContentLength="2000000"/>
              <security mode="None">
                <transport>
                  <extendedProtectionPolicy policyEnforcement="Never" />
                </transport>
              </security>
            </binding>
          </wsHttpBinding>
    
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="True" />
              <serviceDebug includeExceptionDetailInFaults="True" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    

    客户端源:

    private void AddPhoto()
            {
                try
                {
                    Image image = Image.FromFile(WayToFile);
                    var obj = new PhotoData();
                    obj.ID = Guid.NewGuid().ToString();
                    obj.Image = Photo.ImageToByteArray(image);
                    obj.Title = _title;
                    obj.Description = _description;
                    using (var service = new PhotoServiceClient())
                        service.InsertPhoto(obj);
                    AllPhotos.Add(new Photo(obj));
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
    
            public static Image ByteArrayToImage(byte[] byteArrayIn)
            {
                var ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
            public static byte[] ImageToByteArray(Image imageIn)
            {
                var ms = new MemoryStream();
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
    

    服务器端源:

    [DataContract]
    public class PhotoData
    {
        private string _id;
        private string _title;
        private string _description;
        private byte[] _image;
    
        [DataMember]
        public string ID
        {
            set { _id = value; }
            get { return _id; }
        }
    
        [DataMember]
        public string Title
        {
            set { _title = value; }
            get { return _title; }
        }
    
        [DataMember]
        public string Description
        {
            set { _description = value; }
            get { return _description; }
        }
    
        [DataMember]
        public byte[] Image
        {
            set { _image = value; }
            get { return _image; }
        }
    }
    
    [ServiceContract]
    public interface IPhotoService
    {
        [OperationContract(IsOneWay = true)]
        void InsertPhoto(PhotoData photo);
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   The Smallest    15 年前

    看起来好像你没有为服务器设置绑定 在服务器配置中添加 bindingConfiguration="wsHttp" 到线条 <endpoint address="" binding="wsHttpBinding" contract="PhotoService.IPhotoService">

    推荐文章