What I want to do is; I use the "WSDL" service link from the configuration file and use the service programmatically, taking the name of the method I will use.
the code fragment I'm using statically and running is as follows,
jqgrid 4.4.4
And endpoint is,
ServiceName.serviceClientSoapClient= new ServiceName.serviceClientSoapClient();
string xmlStr = client.getValues();
But,I want all this created programmatically, For example; my config file,
<endpoint address="http://someservice.com/Service.asmx"
binding="basicHttpBinding" bindingConfiguration="serviceClientSoap"
contract="ServiceName.serviceClientSoap" name="serviceClientSoap" />
Then i want use this config file, and use service, get results.
I looked at the following links, but here it is done through a single service structure. I want it to be installed from the config file.
How to programmatically connect a client to a WCF service? ,How to: Use the ChannelFactory
答案 0 :(得分:2)
最好创建一个接口并将其实现为服务客户端。通过这种方式,您应该指定配置文件中所需的方法,参数和其他内容,并且难以管理。此外,您不能将结果对象用作已知类型类。
所以,你可以尝试类似的东西:
var url = ConfigurationManager.AppSettings["serviceLink"];
var serviceClientClassName = ConfigurationManager.AppSettings["serviceClientClassName"];
var serviceMethod = ConfigurationManager.AppSettings["serviceMethod"];
var endpoint = new EndpointAddress(new Uri(url));
//Specify the assembly of services library. I am assuming that the services are stored in the Executing Assembly
var serviceClient = Assembly.GetExecutingAssembly().GetTypes()
.FirstOrDefault(x => x.Name == serviceClientClassName);//Find the service client type
var instance = Activator.CreateInstance(serviceClient); //Create a new instance of type
var methodInfo = serviceClient.GetMethod(serviceMethod); //Get method info
var result = methodInfo.Invoke(instance, new object[] {}); // Invoke it
答案 1 :(得分:0)
如果您只需要WCF CommunicationObject来处理RequestReply端点,则以下方法将为您执行此操作。
它接受一个有效的Request消息,以及endpoint和soapaction,并提供服务返回的原始xml。
如果您想要提供其他任何内容,那么Message您需要为使用ServiceContract和OperationContract属性修饰的IRequestChannel实现替代方案。
// give it a valid request message, endpoint and soapaction
static string CallService(string xml, string endpoint, string soapaction)
{
string result = String.Empty;
var binding = new BasicHttpBinding();
// create a factory for a given binding and endpoint
using (var client = new ChannelFactory<IRequestChannel>(binding, endpoint))
{
var anyChannel = client.CreateChannel(); // Implements IRequestChannel
// create a soap message
var req = Message.CreateMessage(
MessageVersion.Soap11,
soapaction,
XDocument.Parse(xml).CreateReader());
// invoke the service
var response = anyChannel.Request(req);
// assume we're OK
if (!response.IsFault)
{
// get the body content of the reply
var content = response.GetReaderAtBodyContents();
// convert to string
var xdoc = XDocument.Load(content.ReadSubtree());
result = xdoc.ToString();
}
else
{
//throw or handle
throw new Exception("panic");
}
}
return result;
}
要使用上述方法,您可以从配置文件中获取这两个参数,或使用一些常量值:
var result = CallService(
@"<GetData xmlns=""http://tempuri.org/""><value>42</value></GetData>",
ConfigurationManager.AppSettings["serviceLink"],
ConfigurationManager.AppSettings["serviceSoapAction"]);
// example without using appSettings
var result2 = CallService(
@"<GetValues xmlns=""http://tempuri.org/""></GetValues>",
"http://localhost:58642/service.svc",
"http://tempuri.org/IService/GetValues");
请注意,您的配置文件中除了以后不需要任何其他配置:
<appSettings>
<add key="serviceLink" value="http://localhost:58642/service.svc"/>
<add key="serviceSoapAction" value="http://tempuri.org/IService/GetData"/>
</appSettings>
使用服务的WSDL来找出soapaction:
<wsdl:binding name="BasicHttpBinding_IService" type="tns:IService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetData">
<soap:operation soapAction="http://tempuri.org/IService/GetData" style="document"/>
并通过其portType和消息的路线,您将找到类型:
<wsdl:types>
<xs:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://schemas.datacontract.org/2004/07/"/>
<xs:element name="GetData">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="value" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
您可以从中为GetData构建XML有效内容的形状:
<GetData xmlns="http://tempuri.org/">
<value>42</value>
</GetData>