Http://stackoverflow.com/questions/730693/translate-this-app-config-xml-to-code-wcf
<system.serviceModel> <bindings> <basicHttpBinding> <binding name= "MyService" closetimeout= "00:01:00" opentimeout= "00:01:00" receivetimeout= "00:10:00" sendtimeout= "00:01:00" allowcookies= "false" Bypassproxyonlocal= "false" Hostnamecomparisonmode= "StrongWildcard" maxbuffersize= "65536" maxbufferpoolsize= "524288" maxreceivedmessagesize= "65536" messageencoding= "Text" textencod ing= "Utf-8" transfermode= "Buffered" usedefaultwebproxy= "true" > <readerqu OTAs maxdepth= "maxstringcontentlength=" 8192 "maxarraylength=" 16384 "maxbytesperread=" 4096 "maxnametablecharcount=" 16384 "/> <security mode= "TranSportwithmessagecredential "> <transport clientcredentialtype=" None "Proxyc Redentialtype= "None" realm= ""/> <message clientcredentialtype= "UserName" algorithmsuite= "Default"/> </security> </binding> </bas ichttpbinding> </bindings> <client> <endpoint address= "Https://server.com/service/MyService.a Smx "binding=" BasicHttpBinding "bindingconfiguration=" MyService "contract=" Myservice.myserviceinterface "NA Me= "MyService"/> </client></system.serviceModel>
Switch
BasicHttpBinding binding = new BasicHttpBinding (); Uri endpointaddress = new Uri ("Https://server.com/service/MyService.asmx"); Channelfactory<myservice.myserviceinterface> factory = new Channelfactory<myservice.myserviceinterface > (binding, EndpointAddress); Myservice.myserviceinterface proxy = factory. CreateChannel ();
Perfect for
BasicHttpBinding binding = new BasicHttpBinding (); Binding. Security.mode = basichttpsecuritymode.transportwithmessagecredential; Uri endpointaddress = new Uri ("Https://server.com/Service.asmx"); Channelfactory<myservice.myserviceinterface> factory = new Channelfactory<myservice.myserviceinterface > (binding, Endpointaddress.tostring ()); Factory. Credentials.UserName.UserName = "UserName"; Factory. Credentials.UserName.Password = "Password"; Myservice.myserviceinterface client = factory. CreateChannel (); Make use of the client to call Web service here ...
Example 2:
<bindings> <customBinding> <binding name= "lbinding" > <security Authenticationmode= "Usernameovertransport" messagesecurityversion= " Wssecurity11wstrustfebruary2005wssecureconversationfebruary2005wssecuritypolicy11 " securityHeaderLayout=" Strict " includetimestamp=" false " requirederivedkeys=" true " keyentropymode=" serverentropy "> </security> <textmessageencoding messageversion= "Soap11"/>
Switch
Uri Epuri = new Uri (_serviceuri); CustomBinding binding = new CustomBinding (); SecurityBindingElement SBE = Securitybindingelement.createusernameovertransportbindingelement (); sbe. Messagesecurityversion = Messagesecurityversion.wssecurity11wstrustfebruary2005wssecureconversationfebruary2005wssecuritypolicy11; Sbe. Securityheaderlayout = Securityheaderlayout.strict;sbe. Includetimestamp = False;sbe. Setkeyderivation (True); sbe. Keyentropymode = system.servicemodel.security.securitykeyentropymode.serverentropy;binding. Elements.add (SBE); binding. Elements.add (New Textmessageencodingbindingelement (MESSAGEVERSION.SOAP11, System.Text.Encoding.UTF8)), binding. Elements.add (New Httpstransportbindingelement ()); EndpointAddress endPoint = new EndpointAddress (Epuri);
Auxiliary functions:
public static TResult Useservice<tchannel, tresult> (string URL, E Ndpointidentity identity, NetworkCredential credential, Func<tchannel, tresult> acc) {var binding = new BasicHttpBinding (); Binding. Security.mode = basichttpsecuritymode.transportcredentialonly; Binding. Security.Transport.ClientCredentialType = httpclientcredentialtype.windows; var endpointaddress = new EndpointAddress (new Uri (URL), identity, new Address Headercollection ()); var factory = new Channelfactory<t> (binding, address); var logincredentials = new ClientCredentials (); LoginCredentials.Windows.ClientCredential = credentials; foreach (Var cred in factory. Endpoint.EndpointBehaviors.Where (b = = B is clientcredentials). ToArray ()) factory. Endpoint.EndpointBehaviors.Remove (cred); Factory. ENDPOINT.ENDPOINTBEHAVIORS.ADD (logincredentials); TChannel channel = Factory. CreateChannel (); BOOL error = TRUE; try {TResult result = ACC (channel); ((Iclientchannel) channel). Close (); Error = FALSE; Factory. Close (); return result; } catch (Exception ex) {Console.WriteLine (ex. ToString ()); return default (TResult); The finally {if (error) ((Iclientchannel) channel). Abort (); }}
Call Method:
Usage
NameSpace.UseService("http://server/XRMDeployment/2011/Deployment.svc", Identity, Credentials,(IOrganizationService context) =>{ ... your code here ... return true;});
示例3:
End point SetupSystem.ServiceModel.EndpointAddress EndPoint = new System.ServiceModel.EndpointAddress ("HTTP/ Domain:port/class/method "); System.ServiceModel.EndpointIdentity endpointidentity = default (System.ServiceModel.EndpointIdentity);//binding setupSystem.ServiceModel.BasicHttpBinding binding = Default (System.ServiceModel.BasicHttpBinding); binding. Transfermode = Transfermode.streamed;//add settingsbinding. maxreceivedmessagesize = Int. Maxvalue;binding. readerquotas.maxarraylength = Int. Maxvalue;binding. readerquotas.maxbytesperread = Int. Maxvalue;binding. readerquotas.maxdepth = Int. Maxvalue;binding. readerquotas.maxnametablecharcount = Int. Maxvalue;binding. maxreceivedmessagesize = Int. Maxvalue;binding. readerquotas.maxstringcontentlength = Int. Maxvalue;binding. messageencoding = wsmessageencoding.text;binding. textencoding = system.text.encoding.utf8;binding. MaxBufferSize = Int. Maxvalue;binding. maxbufferpoolsize = Int. Maxvalue;binding. maxreceivedmessagesize = Int. Maxvalue;binding. SendtImeout = new TimeSpan (0, ten, 0), binding. ReceiveTimeout = new TimeSpan (0, 0);//setup for Custom bindingSystem.ServiceModel.Channels.CustomBinding CustomBinding = new System.ServiceModel.Channels.CustomBinding (binding);
What I did to configure my contract:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), System.ServiceModel.ServiceContractAttribute(Namespace = "http://MyNameSpace", ConfigurationName = "IHostInterface")]public interface IHostInterface{}
Translate this app. config XML to code? (WCF) Z