问题描述
我知道 .NET Core 旨在消耗 REST Apis
并认为 SOAP
之类的东西是旧新闻,但现实是,有很多我们仍然需要使用!
我的应用程序使用了很多,包括显然没有 .netcore 版本的 eBay .NET SDK
和 Despatch Bay,这就是这个问题的内容。
WSDL 文件在这里:https://api.despatchbay.com/soap/v15/account?wsdl
GitHub 文档:https://github.com/despatchbay/despatchbay-api-v15/wiki
我已成功下载此文件,并让 VS 将其添加为 connected service
并创建了相关的 reference.cs
文件。
这会生成一个代理客户端 AccountServicePortTypeClient
我正在尝试运行一个非常基本的查询来获取这样的代码中的帐户余额:
public async Task<float> GetBalance() {
dbAccount.AccountServicePortTypeClient client = new dbAccount.AccountServicePortTypeClient();
client.ClientCredentials.UserName.UserName = "api_user";
client.ClientCredentials.UserName.Password = "api_key";
return (await client.GetAccountBalanceAsync()).AvailableBalance;
}
整个上午都在尝试添加凭据并阅读各种 SO 问题:
- Why TransportWithMessageCredential is not supported in .net core?
- WCFTestClient The HTTP request is unauthorized with client authentication scheme 'Anonymous'
还有更多...我编辑了预先生成的 reference.cs
文件,在生成 EndPoint 绑定的代码中添加了四行:
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.AccountServicePort))
{
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
// NEXT FOUR LInes ARE NEW
result.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
result.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
result.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
result.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
return result;
}
throw new system.invalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.",endpointConfiguration));
}
我还根据上述链接之一确保我使用的是 System.ServiceModel 4.7.*
<ItemGroup>
<packagereference Include="System.ServiceModel.Duplex" Version="4.7.*" />
<packagereference Include="System.ServiceModel.Http" Version="4.7.*" />
<packagereference Include="System.ServiceModel.NetTcp" Version="4.7.*" />
<packagereference Include="System.ServiceModel.Security" Version="4.7.*" />
</ItemGroup>
我尝试了上述的各种组合,但无论我做什么,我都会遇到某种身份验证错误。使用此问题的代码,响应为:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'.
The authentication header received from the server was 'Basic realm="DespatchBay Pro API"'
我不知道为什么它认为我在发布匿名凭据,因为就我而言,我已将其设置为上述。
迫不及待开始尝试使用 eBay API....
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)