我有一个asp.net客户端Web应用程序和一个从架构xsd开发的WCF Web服务。调用服务时,在反序列化请求正文时出现错误。我尝试更新服务参考,但没有帮助。

这是我的代码:

OSEOP.HMA_OrderingBindingClient client = new OSEOP.HMA_OrderingBindingClient();

OSEOP.GetCapabilitiesRequest request = new OSEOP.GetCapabilitiesRequest();
request.GetCapabilities = new OSEOP.GetCapabilities();
request.GetCapabilities.service = "OS";

string[] arrAcceptedVersions = { "1.0.0", "2.0.0" };
request.GetCapabilities.AcceptVersions = arrAcceptedVersions;

OSEOP.Capabilities capabilities = client.GetCapabilities(request.GetCapabilities);


txtGetCapabilitiesResponse.Text = capabilities.Contents.ToString();

client.Close();

这是错误:
System.ServiceModel.FaultException`1 was unhandled by user code
  Message=Error in deserializing body of request message for operation 'GetCapabilities'.
  Source=mscorlib
  StackTrace:
    Server stack trace:
       at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at OSEOP.HMA_OrderingBinding.GetCapabilities(GetCapabilitiesRequest request)
       at OSEOP.HMA_OrderingBindingClient.OSEOP.HMA_OrderingBinding.GetCapabilities(GetCapabilitiesRequest request) in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\oseop_testclient\023fa9f5\ea876945\App_WebReferences.k9c5tqe1.0.cs:line 44135
       at OSEOP.HMA_OrderingBindingClient.GetCapabilities(GetCapabilities GetCapabilities1) in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\oseop_testclient\023fa9f5\ea876945\App_WebReferences.k9c5tqe1.0.cs:line 44141
       at _Default.cmdGetCapabilities_Click(Object sender, EventArgs e) in d:\Documents\DEV\SARPilot\SVN_repository\Services\OrderingServices\TestClient\Default.aspx.cs:line 30
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

如您所见,该错误发生在客户端,并且永远不会发送到WCF服务。因此,我的MessageLogging中没有任何内容。这就是为什么我认为它与服务参考有关。

有人可以帮忙吗?

编辑#1:
我不明白的是,GetCapabilities带有一个GetCapabilitiesRequest参数,但是当我实现客户端时,我的智商要求一个OSEOP.GetCapabilities对象。

OSEOP是我命名的Web参考。
public class OrderingService : HMA_OrderingBinding
{
    public GetCapabilitiesResponse GetCapabilities(GetCapabilitiesRequest request)
    {
        throw new NotImplementedException();
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://www.opengis.net/oseop/1.0", ConfigurationName = "HMA_OrderingBinding")]
public interface HMA_OrderingBinding
{
    [OperationContract]
    [XmlSerializerFormatAttribute]
    GetCapabilitiesResponse GetCapabilities(GetCapabilitiesRequest request);
}



/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.opengis.net/oseop/1.0")]
public partial class Capabilities : CapabilitiesBaseType
{

    private OrderingServiceContentsType contentsField;

    private NotificationProducerMetadataPropertyType notificationsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    public OrderingServiceContentsType Contents
    {
        get
        {
            return this.contentsField;
        }
        set
        {
            this.contentsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    public NotificationProducerMetadataPropertyType Notifications
    {
        get
        {
            return this.notificationsField;
        }
        set
        {
            this.notificationsField = value;
        }
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class GetCapabilitiesRequest
{

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://www.opengis.net/oseop/1.0", Order = 0)]
    public GetCapabilities GetCapabilities;

    public GetCapabilitiesRequest()
    {
    }

    public GetCapabilitiesRequest(GetCapabilities GetCapabilities)
    {
        this.GetCapabilities = GetCapabilities;
    }
}

编辑#2 @Marc:
马克,您的回答很有帮助。但是您会看到服务器端是这样的:
GetCapabilitiesResponse GetCapabilities(GetCapabilitiesRequest request)

但是我的智慧认为这是这样的:
Capabilities GetCapabilities(GetCapabilities GetCapabilities1)

我已经在IOrder.cs文件中找到了一段代码(从架构生成的47,256行代码),我确定是引起问题的原因,但是我尝试注释掉故障功能,更新服务引用以及我的intellisense 仍然想要GetCapabilities GetCapabilities1
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class HMA_OrderingBindingClient : System.ServiceModel.ClientBase<HMA_OrderingBinding>, HMA_OrderingBinding
{

    public HMA_OrderingBindingClient()
    {
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    GetCapabilitiesResponse HMA_OrderingBinding.GetCapabilities(GetCapabilitiesRequest request)
    {
        return base.Channel.GetCapabilities(request);
    }

    public Capabilities GetCapabilities(GetCapabilities GetCapabilities1)
    {
        GetCapabilitiesRequest inValue = new GetCapabilitiesRequest();
        inValue.GetCapabilities = GetCapabilities1;
        GetCapabilitiesResponse retVal = ((HMA_OrderingBinding)(this)).GetCapabilities(inValue);
        return retVal.Capabilities;
    }
}

最佳答案

两个问题:

  • 为什么要创建一个包含子对象GetCapabilitiesRequestGetCapabilities对象,然后在您的方法调用中仅使用包含的子对象GetCapabilities

    那么,为什么不首先创建GetCapabilities而不考虑包装对象呢?
  • 另外,您也可以展示GetCapabilitiesRequestGetCapabilities以及返回类Capabilities吗?如果您有反序列化错误,则最有可能是那些类不正确的问题...

  • 更新:,感谢您对问题的更新...。
    嗯...乍一看似乎找不到任何明显的错误....

    关于您的问题:

    我不明白的是
    GetCapabilities需要一个
    GetCapabilitiesRequest参数,但是
    当我实现客户时,我的
    智慧要求
    OSEOP.GetCapabilities对象。

    是的,这很清楚-您的服务端使用其类集-GetCapabilitiesRequest等。

    当您在Visual Studio中执行Add Service Reference时,VS的作用是
  • 询问服务器以了解服务-它具有什么方法以及需要哪些参数
  • 它为客户端代理创建了一组复制您的类的-在您在Add Service Reference对话框上定义的 namespace 中。这些类看起来与服务器端类完全相同-但是它们是而不是相同的类-它们只是以与服务器端相同的方式序列化为XML(并从XML反序列化)。这就是为什么您的客户端代理在不同的 namespace 中具有不同的类。这是标准的WCF行为-不用担心...

  • 更新编号。 2 :Carlos,您发送给我的架构似乎不完整或有错误。尝试使用OGC project on CodePlex作为基础并手动构建代码,或者等到架构“正式”发布。

    关于asp.net - 反序列化操作的请求消息正文时出现WCF错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4739654/

    10-10 18:25