首页 新闻 会员 周边

对多个WCF服务进行统一连接验证

0
悬赏园豆:50 [已解决问题] 解决于 2010-07-19 18:44

 

先看代码:
代码
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint
= new EndpointAddress(endAddress);
ChannelFactory
<IMyService> myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint);

IMyService wcfClient
= myChannelFactory.CreateChannel();
try
{
IDisposable chanel
= wcfClient as IDisposable;

ICommunicationObject o
= chanel as ICommunicationObject;

o.Opened
+= delegate { Console.WriteLine("OK") };
o.Close();
}
catch (Exception _ex)
{

}

 

问题:以上代码针对IMyService(添加了服务引用)一个WCF服务是可行的,但是假如我有多个WCF服务,需要采用统一的方法验证这些WCF服务是否有效,请问各位有什么好的建议吗?
这里需要指明绑定方式、服务地址还需要添加服务的引用,能不能实现不添加服务的引用,如此我认为就可以统一验证了。

 

 

问题补充: 感谢各位的热心回答! To Rain Shan: 我看了您说的UserNameAuthentication属性验证,代码如下,从代码中可见,我们一样需要添加WCF服务的引用,依然是每个服务都需要对应的验证方法。 // Create a service host. Uri httpUri = new Uri("http://localhost/Calculator"); ServiceHost sh = new ServiceHost(typeof(Calculator), httpUri); // Create a binding that uses a username/password credential. WSHttpBinding b = new WSHttpBinding(SecurityMode.Message); b.Security.Message.ClientCredentialType = MessageCredentialType.UserName; // Add an endpoint. sh.AddServiceEndpoint(typeof(ICalculator), b, "UserNamePasswordCalculator"); // Get a reference to the UserNamePasswordServiceCredential object. UserNamePasswordServiceCredential unpCredential = sh.Credentials.UserNameAuthentication; // Print out values. Console.WriteLine("IncludeWindowsGroup: {0}", unpCredential.IncludeWindowsGroups); Console.WriteLine("UserNamePasswordValidationMode: {0}", unpCredential.UserNamePasswordValidationMode); Console.WriteLine("CachedLogonTokenLifetime.Minutes: {0}", unpCredential.CachedLogonTokenLifetime.Minutes ); Console.WriteLine("CacheLogonTokens: {0}", unpCredential.CacheLogonTokens ); Console.WriteLine("MaxCachedLogonTokens: {0}", unpCredential.MaxCachedLogonTokens ); Console.ReadLine(); To Frank Xu Lei: 非常感谢您,你能给我说下详细的步骤吗?或者提供相关方面的链接,再次感谢!
若问的主页 若问 | 菜鸟二级 | 园豆:405
提问于:2010-07-16 18:17
< >
分享
最佳答案
0

可以,下班了,回头告诉你.

 

随便写一个接口类,如下(如果不小心方法重合了,更好,因为你还可以调用该方法以确定操作存在或运行正常):

[ServiceContract]
 public interface IMyService
{
    [OperationContract()]
    void IsOnline();
}

// 下面是根据你的代码修改的调用:

            string endAddress = @"net.tcp://192.168.6.110:28000/BusinessServices/SMSNotificationService";
            NetTcpBinding myBinding = new NetTcpBinding();
            myBinding.Security.Mode = SecurityMode.None;
            EndpointAddress myEndpoint = new EndpointAddress(endAddress);        
            ChannelFactory<IMyService> myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint);        
            IMyService wcfClient = myChannelFactory.CreateChannel();
            try
            {
                IDisposable chanel = wcfClient as IDisposable;
                ICommunicationObject o = chanel as ICommunicationObject;
                o.Open();
                o.Close();
            }
            catch (EndpointNotFoundException notFound)
            {
                // 这里就表示你要使用的服务不在线,或不存在。
            }
            catch (TimeoutException timeout)
            {
                // 超时了,可能是网络问题,可以增加超时重新调用。
            }
            catch (FaultException fault)
            {
                // 你指定的客户端的配置有些问题,比如是binding,或security的要求。
            }
            catch (Exception _ex)
            {
                // 别的错误,分析下,看看到底是什么问题。
            }

收获园豆:30
Launcher | 高人七级 |园豆:45045 | 2010-07-16 18:22
也就是说,如果你只是要验证你的服务是不是存在的话,你不用"添加服务引用",你可以使用一个客户端自定义的 IMyService 服务契约即可验证,你所要做的只是对你所有的服务更换地址和绑定设置,而这些都可以写在配置文件中,然后用一个循环去依次验证每个服务是否存在.
Launcher | 园豆:45045 (高人七级) | 2010-07-19 14:33
其他回答(4)
0

搞个服务来专门负责认证吧

 

认证之后发个标志,令牌之类的东西,

Virus-BeautyCode | 园豆:1619 (小虾三级) | 2010-07-16 18:39
0

WCF 本身就有验证机制

在 bindings 的配置节点下,配置下就可以了

在behaviors节点下配置 userNameAuthentication

例如:<userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="AspNetSqlMembershipProvider"/>

每次请求,你都要发送用户名和密码进行验证。

你可以去MSDN看一下,或者看看WEBCAST 上WCF方面的 视频。

收获园豆:10
AirSend | 园豆:522 (小虾三级) | 2010-07-16 21:50
0

可以统一,参考Rain Shan的回答。

即使你有多个服务,但是所有的服务都配置相同的服务行为,就可以了。

这个服务行为就是指定相同安全验证的地方。

呵呵

收获园豆:10
Frank Xu Lei | 园豆:1860 (小虾三级) | 2010-07-17 13:35
0

谢谢提问题的人。

有一点难 | 园豆:225 (菜鸟二级) | 2012-06-04 18:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册