对于WCF的几个问题

acqy acqy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想请问几个WCF的问题:
1、如果Service部分没有暴露metadata endpoints,Client部分是否可以使用WsdlImporter以及ChannelFactory动态调用Service?比如,如果Client使用下面的代码去动态调用一个File Download Service,那么在Service部分是否需要暴露metadata endpoint?

static void Main(string[] args)
{
EndpointAddress mexAddress = new EndpointAddress("http://localhost:8889/FileTransfer/mex");
MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);
MetadataSet metadataSet = mexClient.GetMetadata();
WsdlImporter importer = new WsdlImporter(metadataSet);
ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
ChannelFactory<IFileDownloader> factory = new ChannelFactory<IFileDownloader>(
endpoints[0].Binding, endpoints[0].Address);
IFileDownloader downloader = factory.CreateChannel();
DownloadRequest request = new DownloadRequest("C:\\VIRTPART.DAT");
DownloadResponse response = downloader.Download(request);
Console.WriteLine(response.FileName);
Console.WriteLine(response.Size);
}


2、如果上面的回答是“必须暴露”,那么,如果我希望我的Service始终能够被动态调用,那暴露这个metadata endpoint是否会有安全威胁?如何解决这个矛盾?

3、Binding的设置问题。
比如,我的服务端是采用代码动态配置Service的,代码如下:

static void Main(string[] args)
{
ContractDescription cd = ContractDescription.GetContract(typeof(IFileDownloader));
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.MaxReceivedMessageSize = 10067108864;
basicHttpBinding.TransferMode = TransferMode.Streamed;
basicHttpBinding.MessageEncoding = WSMessageEncoding.Mtom;
//WSHttpBinding basicHttpBinding = new WSHttpBinding();
ServiceEndpoint endpoint = new ServiceEndpoint(
cd,
basicHttpBinding,
new EndpointAddress("http://localhost:8889/FileTransfer/"));

ContractDescription mexCd = ContractDescription.GetContract(typeof(IMetadataExchange));
ServiceEndpoint mexEndpoint = new ServiceEndpoint(
mexCd,
MetadataExchangeBindings.CreateMexHttpBinding(),
new EndpointAddress("http://localhost:8889/FileTransfer/mex"));


System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FileTransferImpl));
host.Opened += (a, b) => Console.WriteLine("Service Opened.");

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri("http://localhost:8889/FileTransfer/mex");
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(endpoint.Contract.ContractType, endpoint.Binding, endpoint.Address.ToString());
host.AddServiceEndpoint(mexEndpoint.Contract.ContractType, mexEndpoint.Binding, mexEndpoint.Address.ToString());
host.Open();
foreach (ServiceEndpoint ep in host.Description.Endpoints)
{
Console.WriteLine(ep.Address.ToString());
}
Console.ReadLine();
}

而客户端是用的第一个问题中的代码。其实事实上Service部分暴露了一个BasicHttpBinding的endpoint,并且设置了这个binding的MaxReceivedMessageSize以及TransferMode,但是我在Client端调试的时候,发现importer.ImportAllEndpoints调用所获得的binding虽然也是BasicHttpBinding,但是上述属性的设置值都遗失了(变成了默认值),这是什么原因呢?有没有办法解决?

希望高人出手相救!

...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
acqy
C# 111,129 成员 642,539 内容