简体   繁体   中英

WCF throwing type mismatch error

I have the following wcf function using EF6:

public Order GetNextOrderNotDownloaded()
{
    return _context.Orders
                .Include(o => o.OrderLines)
                .Where(o => !o.IsDownloaded)
                .OrderBy(o => o.DateCreated)
                .FirstOrDefault();
}

But when I try to invoke this in the test client I get an exception thrown saying the connection is being terminated. Through following other posts I added diagnostics to the web.config and found the exception was

Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; charset=utf-8. The client and service bindings may be mismatched.

I've tried various things like changing the binding in the web.config and removing the textEncoding="utf-8" and then other posts suggested it may be a serialisation problem so I tried adding the [Serializable] attribute to the Order and OrderLines classes but that didn't work either.

If I remove the .Include(o => o.OrderLines) it will return me the Order object but I need those orderliness too.

Does anyone know how to solve this problem?

Web.config binding:

<basicHttpBinding>
    <binding name="basicHttpBinding_Service" 
                closeTimeout="00:11:00" openTimeout="00:11:00" receiveTimeout="00:10:00" sendTimeout="00:11:00" 
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
        <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
            <message clientCredentialType="UserName" algorithmSuite="Default"/>
        </security>
    </binding>
</basicHttpBinding>

Further to this, if I recreate the Order and Orderline classes without links to EF (that is without data annotations and virtual properties) and populate them with the values got from the entities, the WCF will start working so why would the "clone" classes work but the EF classes don't

Create a OrderDTO object with contains Oder property and IEnumerable of Orderline

Public class OrderDTO 
{
    Int ID;
    //......
    IEnumerable<Orderline> Orders;
}

Change the return type to OrderDTO

public OrderDTO GetNextOrderNotDownloaded()

And in the LINQ query create lambda expression for Select query see it @ click here Your final code will look like this.

public OrderDTO GetNextOrderNotDownloaded()
{
    return _context.Orders
                .Include(o => o.OrderLines)
                .Where(o => !o.IsDownloaded)
                .OrderBy(o => o.DateCreated)
                .Select(o => new OrderDTO { Id = o.Id, Orders = o.OrderLines})
                .FirstOrDefault();
}

text/xml is used by SOAP 1.1 while soap+xml by SOAP 1.2. In .NET, SOAP 1.1 is used by BasicHttpBinding while SOAP 1.2 by WSHttpBinding. Does your test client use WsHttpBinding?

Your service can provide endpoints that support both protocols but your clients should connect to the endpoint that supports the protocol version they want to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM