несоответствие между сервером и клиентом

#wcf

#wcf

Вопрос:

У меня есть служба отдыха WCF. Я создал его с помощью приложения-службы 4.0 rest, поэтому оно не поддерживает SVC.

У меня есть этот контракт на обслуживание:

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


    [WebGet(UriTemplate = "/Login/?username={username}amp;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 amp;amp; 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:inetpubwwwrootUploadtest.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>
  

Служба входа в систему работает очень хорошо, но у меня возникла проблема с действием загрузки. Я вызываю это через приложение для Android через http://www.myhost.com/Upload и я получаю эту ошибку:

 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:

Итак, получается, что мне нужно было использовать webHttpBinding для обеих конечных точек, а не только для входа в систему.