简体   繁体   English

WCF投掷类型不匹配错误

[英]WCF throwing type mismatch error

I have the following wcf function using EF6: 我有以下使用EF6的wcf函数:

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 通过关注其他帖子,我将诊断添加到了web.config中,发现异常是

Content Type application/soap+xml; 内容类型application / soap + xml; charset=utf-8 was sent to a service expecting text/xml; charset = utf-8已发送到需要text / xml的服务; charset=utf-8. 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. 我已经尝试过各种方法,例如更改web.config中的绑定并删除textEncoding="utf-8" ,然后其他帖子建议这可能是序列化问题,因此我尝试将[Serializable]属性添加到OrderOrderLines类,但这也不起作用。

If I remove the .Include(o => o.OrderLines) it will return me the Order object but I need those orderliness too. 如果删除.Include(o => o.OrderLines) ,它将返回Order对象,但我也需要这些有序性。

Does anyone know how to solve this problem? 有谁知道如何解决这个问题?

Web.config binding: Web.config绑定:

<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 除此之外,如果我重新创建不具有EF链接(即没有数据注释和虚拟属性)的OrderOrderline类,并使用从实体获取的值填充它们,那么WCF将开始工作,为什么“克隆”类会如此工作,但EF班不

Create a OrderDTO object with contains Oder property and IEnumerable of Orderline 创建一个包含Oder属性和Orderline的IEnumerable的OrderDTO对象

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

Change the return type to OrderDTO 将退货类型更改为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. 然后在LINQ查询中为Select查询创建lambda表达式,请参见@ 单击此处。最终代码将如下所示。

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. SOAP 1.1使用text/xml ,而SOAP 1.2使用soap+xml In .NET, SOAP 1.1 is used by BasicHttpBinding while SOAP 1.2 by WSHttpBinding. 在.NET中,BasicHttpBinding使用SOAP 1.1,而WSHttpBinding使用SOAP 1.2。 Does your test client use WsHttpBinding? 您的测试客户端是否使用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. 您的服务可以提供支持这两种协议的端点,但是您的客户端应该连接到支持他们要使用的协议版本的端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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