添加方法以生成 WCF 客户端代理代码

发布于 2024-09-03 17:17:41 字数 236 浏览 11 评论 0原文

我想在 WCF 客户端代理代码(即从 ClientBase 派生的生成的类)中为每个服务操作添加一个附加方法。我编写了一个具有 IOperationContractGenerationExtension 实现的 Visual Studio 扩展,但该接口似乎仅公开了修改服务接口的功能,而不是 ClientBase 派生类。

有没有办法在代理客户端类中生成新方法?

I'd like to add one additional method for each service operation in my WCF client proxy code (i.e. the generated class that derives from ClientBase). I have written a Visual Studio extension that has an IOperationContractGenerationExtension implementation, but this interface only seems to expose the ability to modify the service interface, not the ClientBase-derived class.

Is there any way to generate new methods in the proxy client class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-09-10 17:17:41

据我所知,这些类始终是部分类

public partial class MyWCFServiceClient : ClientBase<IMyWCFService>, IMyWCFService 
{
  ...
}

因此您可以使用自己的第二个文件轻松扩展它们,该文件将方法添加到同一部分类:

YourOwnFile.cs >

public partial class MyWCFServiceClient 
{
   public void NewMethod1()
   {
   }

   public void NewMethod2()
   {
   }
}

As far as I know, those classes are always partial classes:

public partial class MyWCFServiceClient : ClientBase<IMyWCFService>, IMyWCFService 
{
  ...
}

so you can easily extend them with your own, second file that adds method to the same partial class:

YourOwnFile.cs

public partial class MyWCFServiceClient 
{
   public void NewMethod1()
   {
   }

   public void NewMethod2()
   {
   }
}
始于初秋 2024-09-10 17:17:41

我通过在导入过程中为 ClientBase 派生类生成一个包装类来解决这个问题。实际上,我首先尝试生成一个与客户端类同名的附加分部类,但这导致其余的代码生成停止正常工作。

所以我最终生成的代码看起来像这样:(

由内置 WCF 代理生成器生成):(

public interface ServiceReference1
{
    IAsyncResult BeginWebMethod1(AsyncCallback callback, object asyncState);
    void EndWebMethod1(IAsyncResult result);

    IAsyncResult BeginWebMethod2(AsyncCallback callback, object asyncState);
    void EndWebMethod2(IAsyncResult result);

    // ...
}

public class ServiceReference1Client
{
    public event EventHandler<AsyncCompletedEventArgs> WebMethod1Completed;
    public event EventHandler<AsyncCompletedEventArgs> WebMethod2Completed;

    public void WebMethod1Async() { /* ... */ }
    public void WebMethod2Async() { /* ... */ }

    // ...
}

由我的自定义 IOperationContractGenerationExtension 生成):

public class ServiceReference1Wrapper
{
    private ServiceReference1Client _client;

    public ServiceReference1Wrapper(ServiceReference1Client client)
    {
        _client = client;
    }

    public IObservable<AsyncCompletedEventArgs> WebMethod1()
    {
        _client.WebMethod1Async();
        // ...
    }

    public IObservable<AsyncCompletedEventArgs> WebMethod2()
    {
        _client.WebMethod2Async();
        // ...
    }

    // ...
}

注意:我使用的是 Silverlight,所以这就是为什么一切都是异步的。

I got around this by generating a wrapper class for the ClientBase-derived class during the import process. I actually first tried generating an additional partial class with the same name as the client class, but that caused the rest of the code generation to stop working properly.

So my final generated code looks something like:

(generated by the built-in WCF proxy generator):

public interface ServiceReference1
{
    IAsyncResult BeginWebMethod1(AsyncCallback callback, object asyncState);
    void EndWebMethod1(IAsyncResult result);

    IAsyncResult BeginWebMethod2(AsyncCallback callback, object asyncState);
    void EndWebMethod2(IAsyncResult result);

    // ...
}

public class ServiceReference1Client
{
    public event EventHandler<AsyncCompletedEventArgs> WebMethod1Completed;
    public event EventHandler<AsyncCompletedEventArgs> WebMethod2Completed;

    public void WebMethod1Async() { /* ... */ }
    public void WebMethod2Async() { /* ... */ }

    // ...
}

(generated by my custom IOperationContractGenerationExtension):

public class ServiceReference1Wrapper
{
    private ServiceReference1Client _client;

    public ServiceReference1Wrapper(ServiceReference1Client client)
    {
        _client = client;
    }

    public IObservable<AsyncCompletedEventArgs> WebMethod1()
    {
        _client.WebMethod1Async();
        // ...
    }

    public IObservable<AsyncCompletedEventArgs> WebMethod2()
    {
        _client.WebMethod2Async();
        // ...
    }

    // ...
}

Note: I'm using Silverlight, so that's why everything is async.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文