简体   繁体   English

WSDL的.NET Web服务客户端

[英].NET webservice client from WSDL

I have a WSDL from which I generated the implementation of ClientBase namely MyService 我有一个WSDL,通过它我生成了ClientBase的实现,即MyService


function void updateData(Data data){
   BasicHttpBinding binding = new BasicHttpBinding();
    // see there is naked username and password.           
   EndpointAddress address = new EndpointAddress("http://qa.farwaha.com/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&UserName=john&Password=johspassword");
   MyService service = new MyService(binding, address);
   try{
     service.update(data);
   }finally{
     service.close();
   }
}

Unfortunately, to call this web service I have to pass User name and password as shown in the code. 不幸的是,要调用此Web服务,我必须传递代码中所示的用户名和密码。 so, my question is around best practices. 因此,我的问题是围绕最佳实践。

Given that its a Winform Application. 鉴于其Winform应用程序。

  1. How memory / CPU intensive is creating MyService object? 创建MyService对象的内存/ CPU占用率如何?

  2. If you suggest cashing the service, it will hold on to the EndpointAddress; 如果您建议兑现服务,它将保留在EndpointAddress上; which intern has a string with Username and Password. 该实习生的字符串包含用户名和密码。 Which is not a good idea .. any work arounds? 哪个不是一个好主意..任何解决方法?

  3. If I keep the code as such, service object will be garbage collected .. and there will be no trace of user name or password (as soon as GC runs) 如果我将代码保持原样,服务对象将被垃圾回收..并且将不会跟踪用户名或密码(GC运行后)

This is a sample code, I have User Object which stores password in SecureString and every time I have to access the password; 这是一个示例代码,我有一个用户对象,该对象将密码存储在SecureString中,并且每次必须访问密码时都可以; I get string from SecureString in an instance private method, use it quickly and let it be garbage collected. 我在实例私有方法中从SecureString获取字符串,快速使用它并让它被垃圾回收。 I believe if I use a method something like above, it will be safe OR safe enough rather than holding on to reference of Service, What do you suggest !! 我相信,如果我使用上述方法,那将是安全或足够安全的,而不是坚持引用服务,您对此有何建议!

To your specific questions: 对您的具体问题:

  1. In your client code, what you're constructing are instances of lightweight proxy classes that wrap the channel infrastructure that serialize messages to/from the service's endpoints. 在您的客户端代码中,您正在构造的是轻量级代理类的实例,这些实例包装了通道基础结构,这些通道基础结构将消息与服务端点之间进行序列化。 As such, these client proxy classes are cheap and fast to construct because they don't generally do a great deal until you actually send something to the service. 因此,这些客户端代理类便宜且快速构建,因为它们通常在您实际向服务发送内容之前不会做很多事情。 One thing to watch out for is when you call services which employ a more complex security scheme - establishing connections to such services can be costly and so it's worth caching or re-using such connections if you can. 需要注意的一件事是,当您调用采用更复杂安全方案的服务时-建立与此类服务的连接可能会非常昂贵,因此如果可以的话,值得缓存或重新使用此类连接。
  2. "Any workarounds"? “任何解决方法”? Nope! 不! Alas, the service you're consuming is poorly designed - not only do they require username and password to be supplied as part of the service method invocation, but they require that you pass them in the clear over HTTP. las,您使用的服务设计不当-不仅要求将用户名和密码作为服务方法调用的一部分提供,而且要求您通过HTTP明确传递它们。 You might want to ask them to AT LEAST provide an SSL endpoint so that the username and password can be secured during transit. 您可能希望让他们至少提供一个SSL端点,以便在传输过程中可以保护用户名和密码。 Better still, they could implement basic-auth to allow you to acquire an HTTP auth cookie that you can attach to subsequent calls against their services. 更好的是,他们可以实现basic-auth,以允许您获取HTTP auth cookie,您可以将其附加到针对他们的服务的后续调用。
  3. Yes, the GC will eventually clean-up your proxy instances. 是的,GC最终将清理您的代理实例。 Better still, you could wrap your instances in using statements to invoke the Dispose pattern and clean-up deterministically. 更好的是,您可以将实例包装在using语句中,以调用Dispose模式并确定性地进行清理。 See my Magic8Ball WCF Service on Codeplex for examples. 有关示例,请参阅我在Codeplex上的Magic8Ball WCF服务

Other observations: 其他观察:

  1. Because your service requires your username and passoword, each time you call it, you need to pay some very careful thought to how you're going to obtain and store the username and password. 由于您的服务需要用户名和密码,因此每次调用时,都需要仔细考虑如何获取和存储用户名和密码。
  2. I would urge you to specify your binding information in the app.config rather than inline in your code. 我敦促您在app.config中指定绑定信息,而不是在代码中内联。 Again, see the Magic8Ball WCF Service : If you create bindings in code and the endpoint changes or if they open up a new endpoint, protocol, encoding and/or binding, you'll have to recompile and redist your entire app. 再次,请参见Magic8Ball WCF服务 :如果您在代码中创建绑定并且端点发生更改,或者如果它们打开了新的端点,协议,编码和/或绑定,则必须重新编译并重新分配整个应用程序。 If you specify your bindings in config, you might just be able to get away with shipping an updated app.config. 如果您在config中指定绑定,则可能仅能运送更新的app.config。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM