没有配置文件的WCF /服务引用

Spe*_*cer 2 c# wcf

我在使用我的c#应用程序设置Microsoft Translator SOAP Service时遇到问题,而不依赖于它生成的app.config文件.

app.config文件包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_LanguageService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://api.microsofttranslator.com/V2/soap.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_LanguageService"
                contract="Soap.LanguageService" name="BasicHttpBinding_LanguageService" />
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

在参考这个Stack答案时,我使用了以下方法:

internal static Soap.LanguageServiceClient CreateWebServiceInstance()
{
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.Name = "BasicHttpBinding_LanguageService";
    binding.TextEncoding = System.Text.Encoding.UTF8;
    return new Soap.LanguageServiceClient(binding, new EndpointAddress("http://api.microsofttranslator.com/V2/soap.svc"));
}
Run Code Online (Sandbox Code Playgroud)

但是,即使我CreateWebServiceInstance()在执行翻译服务之前打电话,我也会得到这个未处理的异常:

Could not find default endpoint element that references contract 'Soap.LanguageService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Run Code Online (Sandbox Code Playgroud)

我不熟悉BasicHttpBinding,所以我不知道从哪里开始......

wou*_*rvs 8

先问几个问题.当我读到它时,你正在尝试使用服务.该服务暴露了其WDSL.现在您是否尝试使用它而不使用SVCUtil生成的代理类(例如)/ VisualStudioAddServiceReference?或者您只是尝试在代码中配置绑定,而不是在app.config中?

如果您尝试使用该服务而不生成上面指定的代理类,那么您需要使用动态代理生成.().

否则,如果您生成了代理类,那么您应该实际使用app.config配置绑定,因为它更方便.(例如,在进入生产时考虑更改端点地址.)如果您确实不想使用app.config绑定.然后在你现在这样做的方式我怀疑你忘记了你想要用于绑定的合同.

查看服务的ABC :()

Address
Binding
Contract
Run Code Online (Sandbox Code Playgroud)

你已经有了前两个,我没看到最后一个.

编辑

在更深入地研究您的问题之后.并且还注意到这是一个公共服务器.我能够创建绑定以及与服务进行通信.(除非我得到一个非授权的例外,这是正常的,因为我没有令牌.

要连接到服务:在visual studio中添加服务的服务引用. 添加引用

第二步删除将由上面生成的app.config文件.第三步在某处添加以下代码.

        var binding = new BasicHttpBinding();
        var client = new LanguageServiceClient(binding, new EndpointAddress("http://api.microsofttranslator.com/V2/soap.svc"));
Run Code Online (Sandbox Code Playgroud)

第4步:使用客户端调用方法.在该方法中,您必须添加在注册服务时收到的"AppId".通常从这一点上你不应该再遇到任何问题.

如果你还是会遇到他们,请告诉我.