我目前被分配到一个任务,我需要创建一个WCF,作为3个ASMX服务的代理。目前,这些服务由多个应用程序直接使用。
构建此WCF代理的原因是,我们更容易在一个入口点(WCF代理服务)而不是多个应用程序中更新asmx证书。
我创建了一个普通的.NET类库,它基本上为ASMX服务创建了一个单例实例,但我不确定如何在WCF服务中公开它。
如果你可以指出我可以看到WCF的例子作为消费ASMX服务的代理吗?
以下是我的代码:
public static class Service<T> where T : WebServicesClientProtocol
{
static volatile T _Instance;
static volatile int _NumberOfReference = 0;
static object syncRoot = new object();
static Service() { }
public static T Instance
{
get
{
if (_Instance == null)
{
lock (syncRoot)
{
if (_Instance == null)
_Instance = ServiceProxyHelper.CreateServiceProxy<T>(ConfigValueHelper.GetServiceUrl(typeof(T).Name), ConfigValueHelper.CertificateHashKey);
}
}
return _Instance;
}
}
}
这是我使用的助手:
public static class ServiceProxyHelper
{
public static T CreateServiceProxy<T>(string url, string clientBase64KeyId)
{
var webService = SetSecurityCredentials(clientBase64KeyId, url, typeof(T));
if (webService == null)
return default(T);
return (T)Convert.ChangeType(webService, typeof(T));
}
private static WebServicesClientProtocol SetSecurityCredentials(string clientBase64KeyId, string url, Type serviceType)
{
WebServicesClientProtocol result = null;
result = (WebServicesClientProtocol)Activator.CreateInstance(serviceType, true);
result.Url = url;
//Verify default credentials
if (WebRequest.DefaultWebProxy != null)
{
result.Proxy = WebRequest.DefaultWebProxy;
result.Credentials = CredentialCache.DefaultCredentials;
result.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
}
//Add security credentials to SOAP envelope
var token = ServiceProxyHelper.GetSecurityToken(clientBase64KeyId, url);
if (token == null)
return null;
result.RequestSoapContext.Security.Tokens.Add(token);
result.RequestSoapContext.Security.Elements.Add(new MessageSignature(token));
return result;
}
private static X509SecurityToken GetSecurityToken(string clientBase64KeyId, string url)
{
X509SecurityToken result = null;
X509Certificate x509Certificate = null;
var store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
var isOpen = store.OpenRead();
foreach (X509Certificate certificate in store.Certificates)
{
if (Convert.ToBase64String(certificate.GetKeyIdentifier()) != clientBase64KeyId)
continue;
x509Certificate = certificate;
result = new X509SecurityToken(certificate);
break;
}
if (isOpen)
store.Close();
return result;
}
}
答案 0 :(得分:2)
我在这种情况下我会在WCF中创建一个[ServiceContract]并添加ASMX中我希望客户端访问的所有方法。然后使用WCF端点公开该服务契约。它会像一个门面。希望这对你有意义。