2011-10-04 59 views
0

我们通过向我们的项目添加WCF“服务参考”来使用ASMX服务。当我们这样做时,默认情况下它应该使用DataContractSerializer,如果出现问题,它将回退到XmlSerializer添加WCF服务参考返回到XmlSerializer

我试图迫使产生当代理类,但是当我做到这一点,他们是不完整的,丢失了所有的web服务使用的自定义类的DataContractSerializer(只留下了接口,为肥皂,SoapChannel和SoapClient类)

好吧,出事了,它正在回落到使用XmlSerializer。生成参考时没有看到任何错误或警告。

我怎样才能找出是什么导致DataContractSerializer失败并回落到XmlSerializer

+0

我发现使用wsdl.exe创建代理类更容易。 –

回答

0

长话短说,我们无法强制VS使用DataContractSerializer。相反,我们最终编写了代表Web服务的WCF服务合同。当我们使用服务时,我们通常使用我们的OWN服务合同来创建ChannelFactory。以下是我们用来创建频道的代码。

/// <summary> 
/// A generic webservice client that uses BasicHttpBinding 
/// </summary> 
/// <remarks>Adopted from: http://blog.bodurov.com/Create-a-WCF-Client-for-ASMX-Web-Service-Without-Using-Web-Proxy/ 
/// </remarks> 
/// <typeparam name="T"></typeparam> 
public class WebServiceClient<T> : IDisposable 
{ 
    private readonly T channel; 
    private readonly IClientChannel clientChannel; 

    /// <summary> 
    /// Use action to change some of the connection properties before creating the channel 
    /// </summary> 
    public WebServiceClient(string endpointUrl, string bindingConfigurationName) 
    { 
     BasicHttpBinding binding = new BasicHttpBinding(bindingConfigurationName); 

     EndpointAddress address = new EndpointAddress(endpointUrl); 
     ChannelFactory<T> factory = new ChannelFactory<T>(binding, address); 

     this.clientChannel = (IClientChannel)factory.CreateChannel(); 
     this.channel = (T)this.clientChannel; 
    } 

    /// <summary> 
    /// Use this property to call service methods 
    /// </summary> 
    public T Channel 
    { 
     get { return this.channel; } 
    } 

    /// <summary> 
    /// Use this porperty when working with Session or Cookies 
    /// </summary> 
    public IClientChannel ClientChannel 
    { 
     get { return this.clientChannel; } 
    } 

    public void Dispose() 
    { 
     this.clientChannel.Dispose(); 
    } 
}