星期日, 三月 15, 2009

自动添加WCF服务访问点

class Program { 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(); 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(); } }




string addressRoot = "http://localhost:8000/";

WSHttpBinding customBiding = new WSHttpBinding();
customBiding.Security.Mode = SecurityMode.Message;
customBiding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity(m_AuthConfig.DnsIdentity);

m_Host = new ServiceHost(m_RemoteManager, new Uri(addressRoot));

ServiceEndpoint endPoint = m_Host.AddServiceEndpoint(typeof(IRemoteMonitorManager), customBiding, "");
endPoint.Address = new EndpointAddress(new Uri(addressRoot), identity);

ServiceMetadataBehavior metadataBehavior;
metadataBehavior = m_Host.Description.Behaviors.Find();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
m_Host.Description.Behaviors.Add(metadataBehavior);
}

metadataBehavior.HttpGetEnabled = true;

m_Host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "MEX");
m_Host.Description.Behaviors.Add(GetServiceCredentials());
m_Host.Open();