Through the svcutil.exe command, you can generate the proxy class of the WCF Service, and then configure webconfig to call the proxy class to achieve the effect of calling the service. You can also call the WCF Service by adding a web reference.
However, if there are too many services, you must configure the service address in webconfig one by one. Therefore, we have investigated how to dynamically configure the Host Name of the WCF Service:
Write the wcfservice in 1st region and generate a proxy class through the svcutil.exe command.
2. Copy the proxy class in the project
3. Compile the call code
Public class invokecontext
{
# Region callservice
Private const string wshttpbinding = "wshttpbinding ";
// Public static t createwcfservicebyurl <t> (string URL)
//{
// Return createwcfservicebyurl <t> (URL, "wshttpbinding", "wshttpbinding_ideskboardprovider ");
//}
Public static t createwcfservicebyurl <t> (string URL, string Bing, string wsname)
{
If (string. isnullorempty (URL) throw new notsupportedexception ("this URL isn' t null or empty! ");
Endpointaddress address = new endpointaddress (URL );
Binding binding = createbinding (Bing, wsname );
Channelfactory <t> factory = new channelfactory <t> (binding, address );
Return factory. createchannel ();
}
# Endregion
# Region create Protocol
/// <Summary>
/// Create Protocol
/// </Summary>
/// <Param name = "binding"> protocol name </param>
/// <Returns> </returns>
Public static binding createbinding (string binding, string wsname)
{
Binding bindinginstance = NULL;
If (binding. tolower () = wshttpbinding)
{
Wshttpbinding Ws = new wshttpbinding (securitymode. None );
// Ws. Name = "wshttpbinding_ideskboardprovider ";
WS. Name = wsname;
WS. closetimeout = new timespan (00, 01, 00 );
WS. opentimeout = new timespan (,01, 00 );
WS. receivetimeout = new timespan (, 00 );
WS. sendtimeout = new timespan (,01, 00 );
WS. bypassproxyonlocal = false;
WS. transactionflow = false;
WS. hostnamecomparisonmode = hostnamecomparisonmode. strongwildcard;
WS. Max bufferpoolsize = 524288;
WS. maxcomputemessagesize = 65536;
WS. messageencoding = wsmessageencoding. text;
WS. textencoding = encoding. utf8;
WS. usedefawebwebproxy = true;
WS. allowcookies = false;
WS. readerquotas. maxdepth = 32;
WS. readerquotas. maxstringcontentlength = 8192;
WS. readerquotas. maxarraylength = 16384;
WS. readerquotas. maxbytesperread = 4096;
WS. readerquotas. maxnametablecharcount = 16384;
WS. reliablesession. Ordered = true;
WS. reliablesession. inactivitytimeout = new timespan (, 00 );
WS. reliablesession. Enabled = false;
WS. Security. mode = securitymode. message;
WS. Security. Transport. clientcredentialtype = httpclientcredentialtype. windows;
WS. Security. Transport. proxycredentialtype = httpproxycredentialtype. None;
WS. Security. Transport. realm = "";
WS. Security. Message. clientcredentialtype = messagecredentialtype. windows;
WS. Security. Message. negotiateservicecredential = true;
WS. Security. Message. algorithmsuite = securityalgorithmsuite. default;
WS. Security. Message. establishsecuritycontext = true;
Bindinginstance = ws;
}
Return bindinginstance;
}
# Endregion
}
4. Page call service
Default. aspx. CS:
Public partial class _ default: system. Web. UI. Page
{
Private string url = "http: // localhost: 888/latest test. SVC ";
Private string wshttpbinding = "wshttpbinding ";
Private string wshttpinterface = "wshttpbinding_ideskboardprovider ";
Protected void page_load (Object sender, eventargs E)
{
Ideskboard inface = invokecontext. createwcfservicebyurl <ideskboard> (URL, wshttpbinding, wshttpinterface );
List <t_sys_board> listfaces board = inface. getfaces boards (). tolist ();
Repeater1.datasource = listmediaboard;
Repeater1.databind ();
}
}