无法过滤动态创建代理的 WCF 方法

发布于 2024-10-09 20:49:03 字数 1160 浏览 2 评论 0原文

几个月前,我正在开发一个项目,在下拉列表中显示 WCF 方法及其参数。当时我正在使用“添加服务引用”创建代理,并将服务接口硬编码在代码中。 如何在下拉列表中显示 WCF 中可用的所有方法 但是当我尝试动态创建代理来执行相同的操作时,下面的代码不起作用。请帮助我只显示我定义的方法。

// Using Dynamic Proxy Factory by Vipul Modi @ Microsoft
DynamicProxyFactory factory = new DynamicProxyFactory(txtService.Text);

// endpoints.            
string sContract = "";

foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
    sContract = endpoint.Contract.Name;      //this is the service interface name, IAccountInfoService         
}
DynamicProxy proxy = factory.CreateProxy(sContract);
Type proxyType = proxy.ProxyType;

MethodInfo[] methods = proxyType.GetMethods();
foreach (var method in methods)
{
    //if (method.GetCustomAttributes(typeof(OperationContractAttribute), true).Length == 0)
    //    continue;
    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}

代码注释 method.GetCustomAttributes(typeof(OperationContractAttribute), true).Length 不起作用。它没有显示任何方法。如果我将其注释掉,那么结果就是所有方法和变量。我想将其限制为仅用户定义的方法。

Some months back I was working on a project to display the WCF methods and thier parameters in a dropdown. At that time I was creating a proxy using Add Service Reference and hardcoded the service interface in the code.
How can I show all the methods that are available in my WCF in a dropdown
But when I try to create the proxy dynamically to do the same, the below code doesn't work. Please help me to show only the methods that was defined by me.

// Using Dynamic Proxy Factory by Vipul Modi @ Microsoft
DynamicProxyFactory factory = new DynamicProxyFactory(txtService.Text);

// endpoints.            
string sContract = "";

foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
    sContract = endpoint.Contract.Name;      //this is the service interface name, IAccountInfoService         
}
DynamicProxy proxy = factory.CreateProxy(sContract);
Type proxyType = proxy.ProxyType;

MethodInfo[] methods = proxyType.GetMethods();
foreach (var method in methods)
{
    //if (method.GetCustomAttributes(typeof(OperationContractAttribute), true).Length == 0)
    //    continue;
    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}

The code commented method.GetCustomAttributes(typeof(OperationContractAttribute), true).Length doesn't work. It doesn't show any method. If I comment it out, then the result is all methods and variables. I want to restrict it to only user defined methods.

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

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

发布评论

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

评论(1

黑色毁心梦 2024-10-16 20:49:03

我对 DynamicProxyFactory 一无所知,但查看 http://blogs.msdn.com/b/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx 这让我认为

1)代理实际上并不发出方法与属性。虽然我想你可以自己调整代码来实现这一点,但似乎没有必要。

2)如果你只想要一个方法名称列表,似乎你可以从factory.Contracts获取它

i dont know anything about DynamicProxyFactory but looking at http://blogs.msdn.com/b/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx it makes me think that

1) the proxy doesnt actually emit methods with the attribute. it doesn't seem to have a need to though i suppose you could tweak the code on your own to make that happen.

2) if you just want a list of method names, it seems you can get that from factory.Contracts

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