Тут недавно начал разбираться с WCF. Есть пара вопросов. 1. Файл конфигурции App.Config - его нужно как-то спецально подключать или можно с нуля создать самому написать то что нужно и она его найдет и сама использует ?
2. По поводу работы через TCP. Через HTTP все работает нормуль. Есть у меня примерчик пашет, с конфиг файлом. Захотел сделать без него ничего не получается. Да и с нуля сам с ним тоже вряд-ли сделаю. В общем все просто обычный сервер - типа
Код | static void Main(string[] args) { Type serviceType = typeof(RandomDelayService); ServiceHost host = new ServiceHost(serviceType); NetTcpBinding bindingTcp = new NetTcpBinding(); ServiceEndpoint endPoint = host.AddServiceEndpoint(serviceType, bindingTcp, new Uri("net.tcp://localhost:1555/RandomDelayService")); ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetUrl = new Uri("http://localhost:1556/RandomDelayService"); behavior.HttpGetEnabled = true; host.Description.Behaviors.Add(behavior); host.Open(); Console.WriteLine("Service is ready, press any key to exit."); Console.ReadKey(); }
[ServiceContract] class RandomDelayService { [OperationContract] string RandomDelay() { // hour:minute:second:milliseconds string startTime = DateTime.Now.ToString("hh:mm:ss:ffff"); Random random = new Random(); int wait = random.Next(1, 10); Thread.Sleep(wait * 1000); return string.Format("Started at {0} and waited {1} second(s), the time is {2}", startTime, wait, DateTime.Now.ToString("hh:mm:ss.ffff")); } }
|
Код клиента типа
Код | NetTcpBinding binding = new NetTcpBinding(); EndpointAddress address = new EndpointAddress("net.tcp://localhost:1555/RandomDelayService"); ChannelFactory<IMyClient> channel = new ChannelFactory<IMyClient>(binding, address); IMyClient myClient = channel.CreateChannel(); string rezult = myClient.RandomDelay();
|
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Client { [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [System.ServiceModel.ServiceContractAttribute(Namespace = "http://RandomDelayService", ConfigurationName = "RandomDelayService.IMyClient")] public interface IMyClient { [System.ServiceModel.OperationContractAttribute(Action = "http://RandomDelayService/IMyClient/RandomDelay", ReplyAction = "http://RandomDelayService/IMyClient/RandomDelayResponse")] string RandomDelay(); }
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface ICalculatorChannel : IMyClient, System.ServiceModel.IClientChannel { }
[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class MyClient : System.ServiceModel.ClientBase<Client.IMyClient>, Client.IMyClient { public string RandomDelay() { return base.Channel.RandomDelay(); } } }
|
На последней строке дает ошибку мол несоответствие ContractFilter на EndpointDispatcher. Кароче нессответствие контрактов. Как это дело подправить никто не подскажет ? |