现在客户端的代码是通过添加服务引用的方式自动创建的,同时自动在App.config中添加了配置内容,我想要不需要这些配置内容的用纯代码创建的客户端。
一、服务引用自动添加的文件
进入服务引用自动添加的文件夹,有好几个文件
item.xsd:描述了服务的信息
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="WCFServiceLib" elementFormDefault="qualified" targetNamespace="WCFServiceLib" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyMethod">
<xs:complexType>
<xs:sequence />
</xs:complexType>
</xs:element>
<xs:element name="MyMethodResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="MyMethodResult" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetString">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="count" type="xs:int" />
<xs:element minOccurs="0" name="item" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetStringResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="GetStringResult" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SendString">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="msg" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SendStringResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="SendStringResult" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
item1.xsd:似乎定义了一些基本
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="anyType" nillable="true" type="xs:anyType" />
<xs:element name="anyURI" nillable="true" type="xs:anyURI" />
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary" />
<xs:element name="boolean" nillable="true" type="xs:boolean" />
<xs:element name="byte" nillable="true" type="xs:byte" />
<xs:element name="dateTime" nillable="true" type="xs:dateTime" />
<xs:element name="decimal" nillable="true" type="xs:decimal" />
<xs:element name="double" nillable="true" type="xs:double" />
<xs:element name="float" nillable="true" type="xs:float" />
<xs:element name="int" nillable="true" type="xs:int" />
<xs:element name="long" nillable="true" type="xs:long" />
<xs:element name="QName" nillable="true" type="xs:QName" />
<xs:element name="short" nillable="true" type="xs:short" />
<xs:element name="string" nillable="true" type="xs:string" />
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte" />
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt" />
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong" />
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort" />
<xs:element name="char" nillable="true" type="tns:char" />
<xs:simpleType name="char">
<xs:restriction base="xs:int" />
</xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration" />
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?" />
<xs:minInclusive value="-P10675199DT2H48M5.4775808S" />
<xs:maxInclusive value="P10675199DT2H48M5.4775807S" />
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid" />
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName" />
<xs:attribute name="Id" type="xs:ID" />
<xs:attribute name="Ref" type="xs:IDREF" />
</xs:schema>
Reference.cs:自动生成的类代码(核心)//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WCFClientConsole.ServiceReference1 {
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="WCFServiceLib", ConfigurationName="ServiceReference1.IMyContract")]
public interface IMyContract {
[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/IMyContract/MyMethod", ReplyAction="WCFServiceLib/IMyContract/MyMethodResponse")]
string MyMethod();
[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/IMyContract/MyMethod", ReplyAction="WCFServiceLib/IMyContract/MyMethodResponse")]
System.Threading.Tasks.Task<string> MyMethodAsync();
[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/IMyContract/GetString", ReplyAction="WCFServiceLib/IMyContract/GetStringResponse")]
string GetString(int count, string item);
[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/IMyContract/GetString", ReplyAction="WCFServiceLib/IMyContract/GetStringResponse")]
System.Threading.Tasks.Task<string> GetStringAsync(int count, string item);
[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/IMyContract/SendString", ReplyAction="WCFServiceLib/IMyContract/SendStringResponse")]
string SendString(string msg);
[System.ServiceModel.OperationContractAttribute(Action="WCFServiceLib/IMyContract/SendString", ReplyAction="WCFServiceLib/IMyContract/SendStringResponse")]
System.Threading.Tasks.Task<string> SendStringAsync(string msg);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IMyContractChannel : WCFClientConsole.ServiceReference1.IMyContract, System.ServiceModel.IClientChannel {
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class MyContractClient : System.ServiceModel.ClientBase<WCFClientConsole.ServiceReference1.IMyContract>, WCFClientConsole.ServiceReference1.IMyContract {
public MyContractClient() {
}
public MyContractClient(string endpointConfigurationName) :
base(endpointConfigurationName) {
}
public MyContractClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public MyContractClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public MyContractClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
public string MyMethod() {
return base.Channel.MyMethod();
}
public System.Threading.Tasks.Task<string> MyMethodAsync() {
return base.Channel.MyMethodAsync();
}
public string GetString(int count, string item) {
return base.Channel.GetString(count, item);
}
public System.Threading.Tasks.Task<string> GetStringAsync(int count, string item) {
return base.Channel.GetStringAsync(count, item);
}
public string SendString(string msg) {
return base.Channel.SendString(msg);
}
public System.Threading.Tasks.Task<string> SendStringAsync(string msg) {
return base.Channel.SendStringAsync(msg);
}
}
}
另外App.config:配置了一个绑定和一个终结点
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IMyContract">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.1.155:8001/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IMyContract" contract="ServiceReference1.IMyContract"
name="NetTcpBinding_IMyContract" />
</client>
</system.serviceModel>
</configuration>
现在客户端使用是直接 MyContractClient client = new MyContractClient();
client.GetString((int) count, "A");
client.Close();
1.将Reference.cs里面的内容拷贝到另一个文件中,并删除多余的部分
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace WCFClientConsole.Contracts
{
[ServiceContract(Namespace = "WCFServiceLib")]
public interface IMyContract
{
[OperationContract]
string MyMethod();
[OperationContract]
Task<string> MyMethodAsync();
[OperationContract]
string GetString(int count, string item);
[OperationContract]
Task<string> GetStringAsync(int count, string item);
[OperationContract]
string SendString(string msg);
[OperationContract]
Task<string> SendStringAsync(string msg);
}
public interface IMyContractChannel : IMyContract, IClientChannel
{
}
public partial class MyContractClient : ClientBase<IMyContract>, IMyContract
{
public MyContractClient(Binding binding, EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public string MyMethod()
{
return base.Channel.MyMethod();
}
public Task<string> MyMethodAsync()
{
return base.Channel.MyMethodAsync();
}
public string GetString(int count, string item)
{
return base.Channel.GetString(count, item);
}
public Task<string> GetStringAsync(int count, string item)
{
return base.Channel.GetStringAsync(count, item);
}
public string SendString(string msg)
{
return base.Channel.SendString(msg);
}
public Task<string> SendStringAsync(string msg)
{
return base.Channel.SendStringAsync(msg);
}
}
}
2.删除服务引用文件夹,删除App.config中的配置信息
3.修改客户端使用代码
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
EndpointAddress endpointAddress = new EndpointAddress("net.tcp://192.168.1.155:8001/");
MyContractClient client = new MyContractClient(binding, endpointAddress);
client.GetString((int) count, "A");
client.Close();
好了,可以和之前一样的使用了。
接下来可以写一个界面,在界面上修改地址了。
三、直接使用通道
上面的是通过代理类Client调用服务,可以直接使用通道调用服务。
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
EndpointAddress endpointAddress = new EndpointAddress("net.tcp://192.168.1.155:8001/");
ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>(binding, endpointAddress);
//使用方式1
IMyContract channel1 = factory.CreateChannel();
using (channel1 as IDisposable)
{
channel1.SendString(GetString(count, "A"));
}
//使用方式2
IMyContract channel2 = factory.CreateChannel();
channel2.SendString(GetString(count, "B"));
ICommunicationObject communicationObject = channel2 as ICommunicationObject;
Debug.Assert(communicationObject != null);
communicationObject.Close();
书中还提供了帮助类将这部分代码封装起来,最终调用代码和使用代理类差不多。