[英]Referencing a class library without having to reference the library's service references
我正在使用只能通过Web和服务引用进行访问的第三方系统。 在构建处理自动化处理的Windows服务时,我发现如果解决方案使用了Web /服务引用,则还必须在引用第一个引用的任何解决方案中设置它们。
有办法防止这种情况吗? 我想创建一个类库,其中将包含所有实际的API调用,并将其用作NuGet包,而不必同时为每个项目添加引用。
编辑:这是我目前如何调用API的示例:
internal class ApiAccess
{
private readonly Account_SSPSoapClient _apiAccount;
public ApiAccess()
{
_apiAccount = new Account_SSPSoapClient();
}
public string GetAccountId(string accountName)
{
return _apiAccount.GetID(accountName);
}
}
这是一个问题,但似乎不是问题。
实际上,在所有使用代码的项目中实际上都不需要服务引用-您所需的只是app.config中的一些信息。 具体来说,就是绑定和端点地址。 您可以将它们硬编码为代码,然后就可以引用它了。
最简单的情况:
var request = new MyServiceRequest { /* set properties here */ };
var client = MyServiceReferenceClient(new BasicHttpBinding(), new EndpointAddress(@"https://my.service.com/path/to/service"));
var channel = client.ChannelFactory.CreateChannel();
var result = channel.MyService(request);
您将需要在BasicHttpBinding
上设置一些参数,以匹配app.config文件中的内容,URL也从那里出现。
有关为什么默认情况下不起作用的信息,请参见此答案 。
编辑:对于您的代码,您只需替换new Account_SSPSoapClient();
与以下类似:
new Account_SSPSoapClient(new BasicHttpBinding(), new EndpointAddress(@"https://my.service.com/path/to/service"));
其他所有内容都应该一样,但是它将使用这些值而不是app.config值(这是不带参数的操作)。
在app.config文件中查找类似以下内容:
<bindings>
<basicHttpBinding>
<binding name="LabelRecoveryBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
其中的所有内容都与您可以在上面创建的BasicHttpBinding
对象上设置的属性相对应-大多数都是默认值,但是您可能希望手动设置所有内容以确保安全。
同样地,寻找
<client>
<endpoint address="http://153.2.133.60:48010/xoltws_ship/LBRecovery"
binding="basicHttpBinding" bindingConfiguration="LabelRecoveryBinding"
contract="UPSLabelRecoveryService.LabelRecoveryPortType" name="LabelRecoveryPort" />
</client>
这告诉您要提供给new EndpointAddress
URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.