简体   繁体   English

引用类库而不必引用库的服务引用

[英]Referencing a class library without having to reference the library's service references

I'm working with a 3rd party system that is only accessible through web and service references. 我正在使用只能通过Web和服务引用进行访问的第三方系统。 While building a Windows Service that handles automated processing I have discovered that if web/service references are used by a solution, they must also be set up in any solution that references the first. 在构建处理自动化处理的Windows服务时,我发现如果解决方案使用了Web /服务引用,则还必须在引用第一个引用的任何解决方案中设置它们。

Is there a way to prevent this? 有办法防止这种情况吗? I would like to make a class library that will hold all of the actual API calls and use that as a NuGet package without having to also add the references to every project down the road. 我想创建一个类库,其中将包含所有实际的API调用,并将其用作NuGet包,而不必同时为每个项目添加引用。

EDIT: Here's an example of how I call the API currently: 编辑:这是我目前如何调用API的示例:

internal class ApiAccess
{
    private readonly Account_SSPSoapClient _apiAccount;

    public ApiAccess()
    {
        _apiAccount = new Account_SSPSoapClient();
    }

    public string GetAccountId(string accountName)
    {
        return _apiAccount.GetID(accountName);
    }
}

This is a problem, but it's not the problem that it appears to be. 这是一个问题,但似乎不是问题。

You don't actually need the service reference in all projects which use your code - all you need is some information out of the app.config. 实际上,在所有使用代码的项目中实际上都不需要服务引用-您所需的只是app.config中的一些信息。 Specifically, the binding and endpoint address. 具体来说,就是绑定和端点地址。 You can hardcode them into your code, and then you should be able to reference it just fine. 您可以将它们硬编码为代码,然后就可以引用它了。

The simplest case: 最简单的情况:

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);

You'll want to set some paramenters on the BasicHttpBinding to match what's in the app.config file, and the URL also comes out of there. 您将需要在BasicHttpBinding上设置一些参数,以匹配app.config文件中的内容,URL也从那里出现。

See this answer for why it doesn't work by default. 有关为什么默认情况下不起作用的信息,请参见此答案


Edit: For your code, you'd just replace new Account_SSPSoapClient(); 编辑:对于您的代码,您只需替换new Account_SSPSoapClient(); with something along the lines of: 与以下类似:

new Account_SSPSoapClient(new BasicHttpBinding(), new EndpointAddress(@"https://my.service.com/path/to/service"));

Everything else should go the same, but it'll use those values instead of the app.config values (which is what it does with no parameters). 其他所有内容都应该一样,但是它将使用这些值而不是app.config值(这是不带参数的操作)。

Look in the app.config file for something like: 在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>

Everything in that corresponds to a property you can set on the BasicHttpBinding object created above - most of it is the default values, but you may want to manually set everything just to be safe. 其中的所有内容都与您可以在上面创建的BasicHttpBinding对象上设置的属性相对应-大多数都是默认值,但是您可能希望手动设置所有内容以确保安全。

Likewise, look for 同样地,寻找

<client>
        <endpoint address="http://153.2.133.60:48010/xoltws_ship/LBRecovery"
            binding="basicHttpBinding" bindingConfiguration="LabelRecoveryBinding"
            contract="UPSLabelRecoveryService.LabelRecoveryPortType" name="LabelRecoveryPort" />
</client>

That tells you what URL to supply to the new EndpointAddress . 这告诉您要提供给new EndpointAddress URL。

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

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