在 .net 2 中使用 WCF
Using WCF in .net 2
我有一个连接和使用 WCF 方法的方法,它在 HTTPS 上并且需要 .net 4 中的用户名和密码。
现在我需要做同样的事情,但是在 .Net 2 中,我似乎无法让它工作。我不断收到以下错误。有人可以帮忙吗?
错误
{"The underlying connection was closed: An unexpected error occurred on a receive."}
内部异常
{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
.Net 4 原码:
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
EndpointAddress ea = new EndpointAddress(wcfURL);
var web = new Gateway.GatewayClient(myBinding, ea);
// var web = new Gateway.GatewayClient();
XMLCrypto crypto = new XMLCrypto();
web.ClientCredentials.UserName.UserName = crypto.DecryptString(username);
web.ClientCredentials.UserName.Password = crypto.DecryptString(password);
web.Open();
web.Inbound("HOLog", message.Trim().Replace("\n", "").Replace(@"\", ""));
web.Close();
.Net 2 代码
XMLCrypto crypto = new XMLCrypto();
url = "http://..../gateway/gateway.svc";
userName = crypto.DecryptString(userName);
password = crypto.DecryptString(password);
var web = new Gateway.Gateway();
var credentials = new NetworkCredential(userName, password);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "Basic", credentials);
web.Credentials = credentials;
string returnMessage = web.Inbound("LSOA", " ");
在网上搜索了很长时间并测试了与 WCF 方法对话的不同方式后,我找到了它不起作用的原因。
目前 WCF 设置为使用 wsHttpBinding,现在我知道 .net 2 不支持它。我的解决方法是在 WCF 的 Web.config 中将绑定从 wsHttpBinding 更改为 basicHttpBinding。
为了做到这一点并且不影响使用 WCF 的任何东西,我必须创建一个单独的子域,它将使用具有更正绑定的配置引用 WCF。
"The wsHttpBinding is not compatible with the ASMX-style web references used in .NET 2.0."
我有一个连接和使用 WCF 方法的方法,它在 HTTPS 上并且需要 .net 4 中的用户名和密码。 现在我需要做同样的事情,但是在 .Net 2 中,我似乎无法让它工作。我不断收到以下错误。有人可以帮忙吗?
错误 {"The underlying connection was closed: An unexpected error occurred on a receive."} 内部异常 {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
.Net 4 原码:
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
EndpointAddress ea = new EndpointAddress(wcfURL);
var web = new Gateway.GatewayClient(myBinding, ea);
// var web = new Gateway.GatewayClient();
XMLCrypto crypto = new XMLCrypto();
web.ClientCredentials.UserName.UserName = crypto.DecryptString(username);
web.ClientCredentials.UserName.Password = crypto.DecryptString(password);
web.Open();
web.Inbound("HOLog", message.Trim().Replace("\n", "").Replace(@"\", ""));
web.Close();
.Net 2 代码
XMLCrypto crypto = new XMLCrypto();
url = "http://..../gateway/gateway.svc";
userName = crypto.DecryptString(userName);
password = crypto.DecryptString(password);
var web = new Gateway.Gateway();
var credentials = new NetworkCredential(userName, password);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "Basic", credentials);
web.Credentials = credentials;
string returnMessage = web.Inbound("LSOA", " ");
在网上搜索了很长时间并测试了与 WCF 方法对话的不同方式后,我找到了它不起作用的原因。
目前 WCF 设置为使用 wsHttpBinding,现在我知道 .net 2 不支持它。我的解决方法是在 WCF 的 Web.config 中将绑定从 wsHttpBinding 更改为 basicHttpBinding。
为了做到这一点并且不影响使用 WCF 的任何东西,我必须创建一个单独的子域,它将使用具有更正绑定的配置引用 WCF。
"The wsHttpBinding is not compatible with the ASMX-style web references used in .NET 2.0."