0

我最近创建了一个 WCF 服务,并希望通过 Silverlight 应用程序使用它。为此,我使用 SlSvcUtil (Silverlight 4) 创建必要的客户端类。但是对于每个方法,此工具都会生成一个请求对象,该对象具有该方法通常采用的所有参数的属性

public System.IAsyncResult BeginDepartmentGetAll(DepartmentGetAllRequest request,    System.AsyncCallback callback, object asyncState)
    {
        object[] _args = new object[1];
        _args[0] = request;
        System.IAsyncResult _result = base.BeginInvoke("DepartmentGetAll", _args, callback, asyncState);
        return _result;
    }

public System.IAsyncResult BeginDummy(DummyRequest request, System.AsyncCallback callback, object asyncState)
    {
        object[] _args = new object[1];
        _args[0] = request;
        System.IAsyncResult _result = base.BeginInvoke("Dummy", _args, callback, asyncState);
        return _result;
    }

相应的请求类如下所示:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName = "Dummy", WrapperNamespace ="http://example.com/Namespace", IsWrapped = true)]
public partial class DummyRequest

{

[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://example.com/Namespace", Order = 0)]
public string s;

public DummyRequest()
{
}

public DummyRequest(string s)
{
    this.s = s;
}

}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName = "DepartmentGetAll", WrapperNamespace = "http://example.com/Namespace", IsWrapped = true)]
public partial class DepartmentGetAllRequest
{
   public DepartmentGetAllRequest()
   {
   }
}

在类似的 WCF 项目中,这些方法采用 Web 服务方法的普通参数,而不使用请求对象。如何在没有这些请求对象的情况下生成服务方法?

4

1 回答 1

0

好的,我终于设法解决了这个难题:

slsvcutil 是否生成这些神秘的请求对象取决于 ServiceContractAttribute。

如果你设置:

[ServiceContract]
public interface MyService {
    [OperationContract]
    void MyMethod(Guid id);
}

相应的客户端方法如下所示:

service.BeginMyMethod(id);

但是如果你设置:

[ServiceContract(Namespace = "http://example.com/MyService")]
public interface MyService {
    [OperationContract]
    void MyMethod(Guid id);
}

它看起来像这样:

service.BeginMyMethod(new MyMethodRequest(id));
于 2012-03-26T12:30:49.453 回答