我写了合同,主机和客户,我必须为同一个服务创建额外的端点
CONTRACT iService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceContractLZ
{
[ServiceContract]
public interface IKalkulatorLZ
{
[OperationContract]
LiczbaZ DodajLZ(LiczbaZ n1, LiczbaZ n2);
[OperationContract]
void Funkcja1(String s1);
[OperationContract(IsOneWay = true)]
void Funkcja2(String s2);
}
[DataContract]
public class LiczbaZ
{
string opis = "Liczba zespolona";
[DataMember]
public double czescR;
[DataMember]
public double czescU;
[DataMember]
public string Opis
{
get { return opis; }
set { opis = value; }
}
public LiczbaZ(double czesc_rz, double czesc_ur)
{
this.czescR = czesc_rz;
this.czescU = czesc_ur;
}
}
}
合同服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace WcfServiceContractLZ
{
public class KalkulatorLZ : IKalkulatorLZ
{
public LiczbaZ DodajLZ(LiczbaZ n1, LiczbaZ n2)
{
Console.WriteLine("Wywolanie: DodajLZ");
return new LiczbaZ(n1.czescR + n2.czescR,
n1.czescU + n2.czescU);
}
public void Funkcja1(String s1)
{
Console.WriteLine("{0}: funkcja1 - start", s1);
Thread.Sleep(3000);
Console.WriteLine("{0}: funkcja1 - stop", s1);
return;
}
public void Funkcja2(String s2)
{
Console.WriteLine("{0}: funkcja2 - start", s2);
Thread.Sleep(3000);
Console.WriteLine("{0}: funkcja2 - stop", s2);
return;
}
}
}
主持人计划
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WcfServiceContractLZ;
namespace WcfServiceHostLZ
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress2 = new Uri("http://loclahost:3000");
String baseAddress3 = "net.tcp://localhost:4000/SerwisTCP";
ServiceHost mojHost = new ServiceHost(typeof(KalkulatorLZ));
try
{
ServiceEndpoint endpoint1 = mojHost.Description.Endpoints.Find(new Uri("http://localhost:2000/WcfService1/KalkulatorLZ"));
Console.WriteLine("\nService endpoint {0} contains the following:",
endpoint1.Name);
Console.WriteLine("Binding: {0}", endpoint1.Binding.ToString());
Console.WriteLine("Contract: {0}", endpoint1.Contract.ToString());
Console.WriteLine("ListenUri: {0}", endpoint1.ListenUri.ToString());
Console.WriteLine("ListenUriMode: {0}",endpoint1.ListenUriMode.ToString());
ServiceEndpoint endpoint2 = mojHost.AddServiceEndpoint(typeof(IKalkulatorLZ), new WSHttpBinding(),"KalkulatorLZ2");
Console.WriteLine("\nService endpoint {0} contains the following:",
endpoint2.Name);
Console.WriteLine("Binding: {0}", endpoint2.Binding.ToString());
Console.WriteLine("Contract: {0}", endpoint2.Contract.ToString());
Console.WriteLine("ListenUri: {0}", endpoint2.ListenUri.ToString());
Console.WriteLine("ListenUriMode: {0}", endpoint2.ListenUriMode.ToString());
ServiceEndpoint endpoint3 = mojHost.AddServiceEndpoint(typeof(IKalkulatorLZ), new WSHttpBinding(),baseAddress2);
Console.WriteLine("\nService endpoint {0} contains the following:",
endpoint3.Name);
Console.WriteLine("Binding: {0}", endpoint3.Binding.ToString());
Console.WriteLine("Contract: {0}", endpoint3.Contract.ToString());
Console.WriteLine("ListenUri: {0}", endpoint3.ListenUri.ToString());
Console.WriteLine("ListenUriMode: {0}", endpoint3.ListenUriMode.ToString());
ServiceEndpoint endpoint4 = mojHost.AddServiceEndpoint(typeof(IKalkulatorLZ), new NetTcpBinding(),baseAddress3);
Console.WriteLine("\nService endpoint {0} contains the following:",
endpoint4.Name);
Console.WriteLine("Binding: {0}", endpoint4.Binding.ToString());
Console.WriteLine("Contract: {0}", endpoint4.Contract.ToString());
Console.WriteLine("ListenUri: {0}", endpoint4.ListenUri.ToString());
Console.WriteLine("ListenUriMode: {0}", endpoint4.ListenUriMode.ToString());
mojHost.Open();
Console.WriteLine("*** Serwis 1 jest uruchomiony ***");
ContractDescription cd = ContractDescription.GetContract(typeof(IKalkulatorLZ));
Console.WriteLine("Displaying information for contract: {0}",
cd.Name.ToString());
string configName = cd.ConfigurationName;
Console.WriteLine("\tConfiguration name: {0}", configName);
Type contractType = cd.ContractType;
Console.WriteLine("\tContract type: {0}", contractType.ToString());
bool hasProtectionLevel = cd.HasProtectionLevel;
if (hasProtectionLevel)
{
ProtectionLevel protectionLevel = cd.ProtectionLevel;
Console.WriteLine("\tProtection Level: {0}",
protectionLevel.ToString());
}
string name = cd.Name;
Console.WriteLine("\tName: {0}", name);
string namespc = cd.Namespace;
Console.WriteLine("\tNamespace: {0}", namespc);
OperationDescriptionCollection odc = cd.Operations;
Console.WriteLine("\tDisplay Operations:");
foreach (OperationDescription od in odc)
{
Console.WriteLine("\t\t" + od.Name);
}
SessionMode sm = cd.SessionMode;
Console.WriteLine("\tSessionMode: {0}", sm.ToString());
Console.WriteLine("Nacisnij <ENTER> aby zakonczyc.");
Console.WriteLine();
Console.ReadLine();
mojHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("Wystapil wyjatek: {0}", ce.Message);
mojHost.Abort();
}
}
}
}
HOST app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<services>
<service name="WcfServiceContractLZ.KalkulatorLZ"
behaviorConfiguration="Wydajnosc">
<endpoint address="/KalkulatorLZ" binding="wsHttpBinding"
contract="WcfServiceContractLZ.IKalkulatorLZ">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:2000/WcfService1" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Wydajnosc">
<serviceThrottling
maxConcurrentCalls="5"
maxConcurrentSessions="5"
maxConcurrentInstances="5"
/>
<serviceMetadata
httpGetEnabled="true"
httpGetUrl=""
/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
客户计划
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServiceClientLZ.ServiceReference1;
using WcfServiceContractLZ;
namespace WcfServiceClientLZ
{
class Program
{
static void Main(string[] args)
{
KalkulatorLZClient client = new KalkulatorLZClient();
LiczbaZ lru1 = new LiczbaZ(3, 4);
LiczbaZ lru2 = new LiczbaZ(5, 6);
Console.WriteLine("Klient1:");
Console.WriteLine("...Dodaj LZ (3,4) i (5,6)");
LiczbaZ result2 = client.DodajLZ(lru1, lru2);
Console.WriteLine("...(3,4) + (5,6) = ({0},{1})",
result2.czescR, result2.czescU);
Console.WriteLine("...wywolanie funkcji 1");
client.Funkcja1("Klient 1");
Console.WriteLine("...kontynuacja po funkcji 1");
Console.WriteLine("...wywolanie funkcji 2");
client.Funkcja2("Klient 1");
Console.WriteLine("...kontynuacja po funkcji 2");
KalkulatorLZClient client2 = new KalkulatorLZClient("WSHttpBinding_IKalkulatorLZ2");
Console.WriteLine("...wywolanie funkcji 1");
client2.Funkcja1("Klient 2");
KalkulatorLZClient client3 = new KalkulatorLZClient("WSHttpBinding_IKalkulatorLZ1");
Console.WriteLine("...wywolanie funkcji 1");
client3.Funkcja1("Klient 3");
KalkulatorLZClient client4 = new KalkulatorLZClient("NetTcpBinding_IKalkulatorLZ");
Console.WriteLine("...wywolanie funkcji 1");
client4.Funkcja1("Klient 4");
client.Close();
client2.Close();
client3.Close();
client4.Close();
}
}
}
CLIENT app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IKalkulatorLZ" />
</netTcpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_IKalkulatorLZ" />
<binding name="WSHttpBinding_IKalkulatorLZ1" />
<binding name="WSHttpBinding_IKalkulatorLZ2" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2000/WcfService1/KalkulatorLZ"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IKalkulatorLZ"
contract="ServiceReference1.IKalkulatorLZ" name="WSHttpBinding_IKalkulatorLZ">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="http://localhost:2000/WcfService1/KalkulatorLZ2"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IKalkulatorLZ1"
contract="ServiceReference1.IKalkulatorLZ" name="WSHttpBinding_IKalkulatorLZ1">
<identity>
<userPrincipalName value="KAROLA\Karolina" />
</identity>
</endpoint>
<endpoint address="http://loclahost:3000/" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IKalkulatorLZ2" contract="ServiceReference1.IKalkulatorLZ"
name="WSHttpBinding_IKalkulatorLZ2">
<identity>
<userPrincipalName value="KAROLA\Karolina" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:4000/SerwisTCP" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IKalkulatorLZ" contract="ServiceReference1.IKalkulatorLZ"
name="NetTcpBinding_IKalkulatorLZ">
<identity>
<userPrincipalName value="KAROLA\Karolina" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
在命令promt的结果中,Host运行okey但是客户端给我这个:找到了该合同的多个端点配置