服务器和客户端之间不匹配

时间:2011-10-19 23:49:38

标签: wcf

我有一个WCF休息服务。我使用4.0休息服务应用程序创建它,因此它是SVC-less。

我有这份服务合同:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{


    [WebGet(UriTemplate = "/Login/?username={username}&password={password}", ResponseFormat= WebMessageFormat.Json)]
    public Response Login(string username, string password)
    {

        Response res;
        BillboardsDataContext db = new BillboardsDataContext();
        var q = from lgin in db.logins
                where lgin.username == username && lgin.password == password
                select lgin;
        if (q.Count() != 0)
        {
            res = new Response(true, "Login successful");
            return res;
        }
        else
        {
            res = new Response(false, "Login failed!");
            return res;
        }


    }

    [WebInvoke(UriTemplate = "", Method = "POST")]
    public void Upload(Stream fileStream)
    {
        FileStream targetStream = null;
        string uploadFolder = @"C:\inetpub\wwwroot\Upload\test.jpg";
        using (targetStream = new FileStream(uploadFolder, FileMode.Create,
            FileAccess.Write, FileShare.None))
        {
            const int bufferLen = 65000;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = fileStream.Read(buffer, 0, bufferLen)) > 0)
            {
                targetStream.Write(buffer, 0, count);
            }
            targetStream.Close();
            fileStream.Close();
        }
    }

}

和这个web.config:

<services>
  <service name="BillboardServices.Service1" behaviorConfiguration="Meta">
    <endpoint name="restful" address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="BillboardServices.Service1" />
    <endpoint name="streamFile" address="/Upload" binding="basicHttpBinding" bindingConfiguration="streamBinding" contract="BillboardServices.Service1" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="REST">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Meta">
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceMetadata httpGetEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <basicHttpBinding>
    <binding name="streamBinding" maxReceivedMessageSize="64000" maxBufferSize="64000" transferMode="Streamed" messageEncoding="Mtom">
      <readerQuotas maxDepth="64000" maxStringContentLength="64000" maxArrayLength="64000" maxBytesPerRead="64000" maxNameTableCharCount="64000"/>
    </binding>
  </basicHttpBinding>
</bindings>

登录服务运行良好,但我遇到上传操作的问题。我通过http://www.myhost.com/Upload通过Android应用程序调用它,我收到此错误:

Content Type multipart/form-data; boundary=wjtUI0EFrpQhBPtGne9le5_-yMxPZ_sxZJUrFf- was sent to a service expecting multipart/related; type="application/xop+xml".  The client and service bindings may be mismatched.

我无法找到有关此错误的信息。有人看过这个吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

事实证明,我需要对两个端点使用webHttpBinding,而不仅仅是登录。