public void MultipleClientsUsingPooledSocket() { var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri(NetTcpServiceUri))); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var clientIpEndpoint = channel.GetClientIpEndpoint(); ((IChannel)channel).Close(); for (int i = 0; i < 10; i++) { channel = factory.CreateChannel(); ((IChannel)channel).Open(); var clientIpEndpoint2 = channel.GetClientIpEndpoint(); Assert.Equal(clientIpEndpoint, clientIpEndpoint2); ((IChannel)channel).Close(); } factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public void MultipleClientsUsingPooledSocket() { IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { System.ServiceModel.NetTcpBinding binding = ClientHelper.GetStreamedModeBinding(); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath)); channel = factory.CreateChannel(); ((IChannel)channel).Open(); string clientIpEndpoint = channel.GetClientIpEndpoint(); ((IChannel)channel).Close(); for (int i = 0; i < 10; i++) { channel = factory.CreateChannel(); ((IChannel)channel).Open(); string clientIpEndpoint2 = channel.GetClientIpEndpoint(); Assert.Equal(clientIpEndpoint, clientIpEndpoint2); ((IChannel)channel).Close(); } factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public void MultipleClientsNonConcurrentNetTcpClientConnection() { string testString = new string('a', 3000); var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri(NetTcpServiceUri))); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoString(testString); ((IChannel)channel).Close(); Assert.Equal(testString, result); channel = factory.CreateChannel(); ((IChannel)channel).Open(); result = channel.EchoString(testString); ((IChannel)channel).Close(); Assert.Equal(testString, result); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public static void MultipleClientsNonConcurrentNetTcpClientConnection() { string testString = new string('a', 3000); var host = CreateWebHostBuilder(new string[0]).Build(); using (host) { host.Start(); var binding = new System.ServiceModel.NetTcpBinding { TransferMode = System.ServiceModel.TransferMode.Streamed }; var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:8808/nettcp.svc"))); var channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoString(testString); ((IChannel)channel).Close(); Assert.Equal(testString, result); channel = factory.CreateChannel(); ((IChannel)channel).Open(); result = channel.EchoString(testString); ((IChannel)channel).Close(); Assert.Equal(testString, result); } }
public static void MultipleClientsUsingPooledSocket() { var host = CreateWebHostBuilder(new string[0]).Build(); using (host) { host.Start(); var binding = new System.ServiceModel.NetTcpBinding() { OpenTimeout = TimeSpan.FromMinutes(20), CloseTimeout = TimeSpan.FromMinutes(20), SendTimeout = TimeSpan.FromMinutes(20), ReceiveTimeout = TimeSpan.FromMinutes(20) }; var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:8808/nettcp.svc"))); var channel = factory.CreateChannel(); ((IChannel)channel).Open(); var clientIpEndpoint = channel.GetClientIpEndpoint(); ((IChannel)channel).Close(); for (int i = 0; i < 10; i++) { channel = factory.CreateChannel(); ((IChannel)channel).Open(); var clientIpEndpoint2 = channel.GetClientIpEndpoint(); ((IChannel)channel).Close(); Assert.Equal(clientIpEndpoint, clientIpEndpoint2); } } }
public void FaultContractsVaryName() { var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { host.Start(); var httpBinding = ClientHelper.GetBufferedModeBinding(); var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestFaultContractName1>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/TestFaultContractName1.svc"))); var channel = factory.CreateChannel(); string faultToThrow = "Test fault thrown from a service"; //variation method1 try { string s = channel.Method1(""); } catch (Exception e) { Assert.NotNull(e); Assert.IsType <System.ServiceModel.FaultException <string> >(e); var ex = (System.ServiceModel.FaultException <string>)e; Assert.Equal(faultToThrow, ex.Detail.ToString()); } } }
public void BasicHttpsCustomBindingRequestReplyEchoString(bool useHttps) { string testString = new string('a', 4000); IWebHost host = ServiceHelper.CreateHttpsWebHostBuilder <StartupCustomBinding>(_output).Build(); using (host) { string serviceUrl = (useHttps ? "https" : "http") + "://localhost:8443/BasicHttpWcfService/basichttp.svc"; host.Start(); System.ServiceModel.BasicHttpBinding BasicHttpBinding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.BasicHttpSecurityMode.Transport); var factory = new System.ServiceModel.ChannelFactory <ClientContract.IEchoService>(BasicHttpBinding, new System.ServiceModel.EndpointAddress(new Uri(serviceUrl))); factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication { CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None }; try { ClientContract.IEchoService channel = factory.CreateChannel(); ((IChannel)channel).Open(); string result = channel.EchoString(testString); Assert.Equal(testString, result); ((IChannel)channel).Close(); } catch (Exception ex) { Assert.True(!useHttps && ex.Message.Contains("The provided URI scheme 'http' is invalid")); } } }
public static void ServiceThrowsExceptionDetailsIncludedInFault() { string exceptionMessage = "This is the exception message"; string stackTraceTopMethod = " at ErrorHandling.ThrowingService.Echo(String echo)"; System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <ThrowingDetailInFaultService, ISimpleService>( (services) => { services.AddSingleton(new ThrowingDetailInFaultService(new Exception(exceptionMessage))); }); factory.Open(); ISimpleService channel = factory.CreateChannel(); ((System.ServiceModel.IClientChannel)channel).Open(); System.ServiceModel.FaultException <System.ServiceModel.ExceptionDetail> exceptionThrown = Assert.Throws <System.ServiceModel.FaultException <System.ServiceModel.ExceptionDetail> >(() => { _ = channel.Echo("hello"); }); Assert.NotNull(exceptionThrown); Assert.NotNull(exceptionThrown.Detail); Assert.True(exceptionThrown.Code.IsReceiverFault); System.ServiceModel.ExceptionDetail detail = exceptionThrown.Detail; Assert.Equal(exceptionMessage, detail.Message); Assert.StartsWith(stackTraceTopMethod, detail.StackTrace); Assert.Equal("InternalServiceFault", exceptionThrown.Code.SubCode.Name); ((System.ServiceModel.Channels.IChannel)channel).Close(); factory.Close(); TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory); }
public static void InstanceContextMode_Single() { SingleInstanceContextSimpleService.ClearCounts(); var serviceInstance = new SingleInstanceContextSimpleService(); System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleInstanceContextSimpleService, ISimpleService>( (services) => { services.AddSingleton(serviceInstance); }); factory.Open(); ISimpleService channel = factory.CreateChannel(); ((System.ServiceModel.Channels.IChannel)channel).Open(); Assert.Equal(1, SingleInstanceContextSimpleService.AddBindingParametersCallCount); Assert.Equal(1, SingleInstanceContextSimpleService.ApplyDispatchBehaviorCount); Assert.Equal(1, SingleInstanceContextSimpleService.ValidateCallCount); string echo = channel.Echo("hello"); echo = channel.Echo("hello"); Assert.Equal(1, SingleInstanceContextSimpleService.CreationCount); Assert.Equal(0, SingleInstanceContextSimpleService.DisposalCount); Assert.Equal(2, serviceInstance.CallCount); ((System.ServiceModel.Channels.IChannel)channel).Close(); factory.Close(); TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory); }
public static void ComplexMessage_OperationFormatUseLiteral() { System.ServiceModel.ChannelFactory <IEchoSoapService> factory = DispatcherHelper .CreateChannelFactory <EchoSoapService, IEchoSoapService>(); factory.Open(); IEchoSoapService channel = factory.CreateChannel(); ((System.ServiceModel.Channels.IChannel)channel).Open(); var expected = new ComplexMessage { Date = System.DateTime.Now, InnerMessages = new InnerComplexMessage[] { new InnerComplexMessage { Guid = System.Guid.NewGuid(), }, }, }; ComplexMessage actual = channel.GetComplexMessageLiteral(expected); Assert.Equal(expected, actual); ((System.ServiceModel.Channels.IChannel)channel).Close(); factory.Close(); TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory); }
public void SimpleNetTcpClientConnectionWindowsAuth() { string testString = new string('a', 3000); var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath)); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoString(testString); Assert.Equal(testString, result); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public void TwowayUsingParamsKeyword() { IWebHost host = ServiceHelper.CreateWebHostBuilder <ContractShapeParamsServiceStartup>(_output).Build(); using (host) { host.Start(); System.ServiceModel.BasicHttpBinding httpBinding = ClientHelper.GetBufferedModeBinding(); var factory = new System.ServiceModel.ChannelFactory <ClientContract.IServiceContract_Params>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/ContractShapeParamsService.svc"))); ClientContract.IServiceContract_Params channel = factory.CreateChannel(); int[] nums = { 0, 1, 5, 25 }; foreach (int numberOfParams in nums) { int[] paramVals = new int[numberOfParams]; for (int itemNum = 0; itemNum < numberOfParams; itemNum++) { paramVals[itemNum] = itemNum; } string response = channel.TwoWayParamArray(numberOfParams, paramVals); Assert.Equal($"Service recieved and processed {numberOfParams} args", response); } } }
private string Variation_Service_BaseDataContractMethod(string clientString) { // Create the proxy var httpBinding = ClientHelper.GetBufferedModeBinding(); System.ServiceModel.ChannelFactory <ClientContract.ISanityAParentB_857419_ContractBase> channelFactory = new System.ServiceModel.ChannelFactory <ClientContract.ISanityAParentB_857419_ContractBase>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/basichttp.svc"))); // var clientProxy = this.GetProxy<ClientContract.ISanityAParentB_857419_ContractBase>(); foreach (var operation in channelFactory.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior behavior = operation.OperationBehaviors.FirstOrDefault( x => x.GetType() == typeof(DataContractSerializerOperationBehavior)) as DataContractSerializerOperationBehavior; behavior.DataContractResolver = new ManagerDataContractResolver <ClientContract.MyBaseDataType>(); } var clientProxy = channelFactory.CreateChannel(); // Send the two way message _output.WriteLine("Testing [Variation_Service_BaseTwoWayMethod]"); var dataObj = new ClientContract.MyBaseDataType(); dataObj.data = clientString; var result = (ClientContract.MyBaseDataType)clientProxy.DataContractMethod(dataObj); string response = result.data; _output.WriteLine($"Testing [Variation_Service_BaseTwoWayMethod] returned <{response}>"); return(response); }
public void ConcurrentNetTcpClientConnection() { var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri(NetTcpServiceUri))); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var resultTask = channel.WaitForSecondRequestAsync(); Thread.Sleep(TimeSpan.FromSeconds(1)); channel.SecondRequest(); var waitResult = resultTask.GetAwaiter().GetResult(); Assert.True(waitResult, $"SecondRequest wasn't executed concurrently"); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public void WSHttpRequestReplyWithTransportMessageEchoString() { ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateCertificate); string testString = new string('a', 3000); var host = ServiceHelper.CreateHttpsWebHostBuilder <WSHttpTransportWithMessageCredentialWithUserName>(_output).Build(); using (host) { host.Start(); var wsHttpBinding = ClientHelper.GetBufferedModeWSHttpBinding(System.ServiceModel.SecurityMode.TransportWithMessageCredential); wsHttpBinding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName; var factory = new System.ServiceModel.ChannelFactory <ClientContract.IEchoService>(wsHttpBinding, new System.ServiceModel.EndpointAddress(new Uri("https://localhost:8443/WSHttpWcfService/basichttp.svc"))); ClientCredentials clientCredentials = (ClientCredentials)factory.Endpoint.EndpointBehaviors[typeof(ClientCredentials)]; clientCredentials.UserName.UserName = "******"; clientCredentials.UserName.Password = "******"; factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication(); factory.Credentials.ServiceCertificate.SslCertificateAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; ClientContract.IEchoService channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoString(testString); Assert.Equal(testString, result); Thread.Sleep(5000); ((IChannel)channel).Close(); Console.WriteLine("read "); } }
// [Fact, Description("transport-security-with-certificate-authentication")] //TODO set up in container, tested locally and this works public void WSHttpRequestReplyWithTransportMessageCertificateEchoString() { ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateCertificate); string testString = new string('a', 3000); var host = ServiceHelper.CreateHttpsWebHostBuilder <WSHttpTransportWithMessageCredentialWithCertificate>(_output).Build(); using (host) { host.Start(); var wsHttpBinding = ClientHelper.GetBufferedModeWSHttpBinding(System.ServiceModel.SecurityMode.TransportWithMessageCredential); wsHttpBinding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Certificate; var factory = new System.ServiceModel.ChannelFactory <ClientContract.IEchoService>(wsHttpBinding, new System.ServiceModel.EndpointAddress(new Uri("https://localhost:8443/WSHttpWcfService/basichttp.svc"))); ClientCredentials clientCredentials = (ClientCredentials)factory.Endpoint.EndpointBehaviors[typeof(ClientCredentials)]; clientCredentials.ClientCertificate.SetCertificate( StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName , "birojtestcert" ); ClientContract.IEchoService channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoString(testString); Assert.Equal(testString, result); ((IChannel)channel).Close(); Console.WriteLine("read "); } }
public Interface.DemoResponse DoTheThing(Interface.DemoRequest request) { request.Bar += "-Proxied"; var svc = factory.CreateChannel(); try { return(svc.DoTheThing(request)); } finally { if (svc is System.ServiceModel.ICommunicationObject client) { if (client.State == System.ServiceModel.CommunicationState.Faulted) { client.Abort(); } else { client.Close(); } } } }
public void EchoStringTest(MessageVersion messageVersion, System.ServiceModel.Channels.MessageVersion clientMessageVersion) { string testString = new string('a', 10); var webHostBuilder = ServiceHelper.CreateWebHostBuilder <Startup>(_output); webHostBuilder.ConfigureServices(services => services.AddServiceModelServices()); webHostBuilder.Configure(app => { app.UseServiceModel(builder => { builder.AddService <Services.TestService>(); builder.AddServiceEndpoint <Services.TestService, ServiceContract.ITestService>(Startup.GetServerBinding(messageVersion), "/MessageVersionTest.svc"); }); }); var host = webHostBuilder.Build(); using (host) { host.Start(); var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(Startup.GetClientBinding(clientMessageVersion), new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + "/MessageVersionTest.svc")); ClientContract.ITestService channel = factory.CreateChannel(); var result = channel.EchoString(testString); Assert.Equal(testString, result); ((System.ServiceModel.IClientChannel)channel).Close(); } }
private void assertForCommon(String sourceString, IWebHost host) { using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath)); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoForPermission(sourceString); Assert.Equal(sourceString, result); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public async Task CanFetchHttpHeadersAsync(bool initialYield) { string testString = new string('a', 3000); var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { host.Start(); System.ServiceModel.ChannelFactory <IHttpHeadersService> factory = null; IHttpHeadersService channel = null; try { var httpBinding = ClientHelper.GetBufferedModeBinding(); factory = new System.ServiceModel.ChannelFactory <IHttpHeadersService>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/httpHeaders.svc"))); channel = factory.CreateChannel(); Task <string> resultTask = null; using (new System.ServiceModel.OperationContextScope((System.ServiceModel.IContextChannel)channel)) { var httpRequestMessageProperty = new System.ServiceModel.Channels.HttpRequestMessageProperty(); httpRequestMessageProperty.Headers[TestHeaderName] = TestHeaderValue; System.ServiceModel.OperationContext.Current.OutgoingMessageProperties.Add(System.ServiceModel.Channels.HttpRequestMessageProperty.Name, httpRequestMessageProperty); resultTask = channel.GetHeaderAsync(TestHeaderName, initialYield); } var headerValue = await resultTask; Assert.Equal(TestHeaderValue, headerValue); } finally { ServiceHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory); } } }
public void MuptiOverloadedMethod() { IWebHost host = ServiceHelper.CreateWebHostBuilder <ContractShapeOverloadsServiceStartup>(_output).Build(); using (host) { host.Start(); System.ServiceModel.BasicHttpBinding httpBinding = ClientHelper.GetBufferedModeBinding(); var factory = new System.ServiceModel.ChannelFactory <ClientContract.IServiceContract_Overloads>(httpBinding, new System.ServiceModel.EndpointAddress(new Uri("http://localhost:8080/BasicWcfService/ContractShapeOverloadsService.svc"))); ClientContract.IServiceContract_Overloads channel = factory.CreateChannel(); // Call into the appropriate overload per variation string response = channel.TwoWayMethod(); Assert.Equal("Server Received: Void", response); response = channel.TwoWayMethod(12345); Assert.Equal("Server Received: 12345", response); response = channel.TwoWayMethod("String From Client"); Assert.Equal("Server Received: String From Client", response); var ctToSend = new ClientContract.SM_ComplexType { s = "8675309", n = 8675309 }; response = channel.TwoWayMethod(ctToSend); Assert.Equal("Server Received: 8675309 and 8675309", response); } }
public async Task StreamedSynchronouslyCompletingTask() { string testString = new string('a', 3000); var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <Contract.ITaskService> factory = null; Contract.ITaskService channel = null; host.Start(); try { var binding = ClientHelper.GetStreamedModeBinding(); factory = new System.ServiceModel.ChannelFactory <Contract.ITaskService>(binding, new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:8808" + Startup.StreamedRelatveAddress))); channel = factory.CreateChannel(); ((IChannel)channel).Open(); await channel.SynchronousCompletion(); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public static List <DeliveryMen> GetDeliveryMens() { var factory = new System.ServiceModel.ChannelFactory <IDeliveryService>("DeliveryService"); try { var channel = factory.CreateChannel(); var dtoDeliveryMens = channel.GetDeliveryMens(); factory.Close(); if (dtoDeliveryMens == null) { return(new List <DeliveryMen>()); } var deliveryMens = dtoMapper.Map <DTObjects.DeliveryMen[], DeliveryMen[]>(dtoDeliveryMens).ToList(); return(deliveryMens); } catch (Exception ee) { if (factory != null) { factory.Abort(); } return(new List <DeliveryMen>()); } }
internal void WSHttpRequestReplyWithTransportMessageCertificateEchoString() { string testString = new string('a', 3000); IWebHost host = ServiceHelper.CreateHttpsWebHostBuilder <WSHttpTransportWithMessageCredentialWithCertificate>(_output).Build(); using (host) { host.Start(); System.ServiceModel.WSHttpBinding wsHttpBinding = ClientHelper.GetBufferedModeWSHttpBinding(System.ServiceModel.SecurityMode.TransportWithMessageCredential); wsHttpBinding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Certificate; var factory = new System.ServiceModel.ChannelFactory <ClientContract.IEchoService>(wsHttpBinding, new System.ServiceModel.EndpointAddress(new Uri("https://localhost:8443/WSHttpWcfService/basichttp.svc"))); ClientCredentials clientCredentials = (ClientCredentials)factory.Endpoint.EndpointBehaviors[typeof(ClientCredentials)]; clientCredentials.ClientCertificate.Certificate = ServiceHelper.GetServiceCertificate(); factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication { CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None }; ClientContract.IEchoService channel = factory.CreateChannel(); ((IChannel)channel).Open(); string result = channel.EchoString(testString); Assert.Equal(testString, result); ((IChannel)channel).Close(); Console.WriteLine("read "); } }
/// <summary> /// 创建远程服务接口 /// </summary> /// <typeparam name="T">类型</typeparam> /// <returns>远程服务接口</returns> private static T CreateServiceClient <T>() { Type type = typeof(T); var factory = new System.ServiceModel.ChannelFactory <T>(type.FullName); return(factory.CreateChannel()); }
public void MessageContract() { var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri(NetTcpServiceUri))); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var message = new ClientContract.TestMessage() { Header = "Header", Body = new MemoryStream(Encoding.UTF8.GetBytes("Hello world")) }; var result = channel.TestMessageContract(message); Assert.Equal("Header from server", result.Header); Assert.Equal("Hello world from server", new StreamReader(result.Body, Encoding.UTF8).ReadToEnd()); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public static void InstanceContextMode_PerCall() { PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts(); System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <PerCallInstanceContextSimpleServiceAndBehavior, ISimpleService>( (services) => { services.AddTransient <PerCallInstanceContextSimpleServiceAndBehavior>(); }); factory.Open(); ISimpleService channel = factory.CreateChannel(); ((System.ServiceModel.Channels.IChannel)channel).Open(); // Instance created as part of service startup to probe if type is availale in DI Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount); // Instance not disposed as it implements IServiceBehavior and is added to service behaviors Assert.Equal(0, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount); Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.AddBindingParametersCallCount); Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.ApplyDispatchBehaviorCount); Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.ValidateCallCount); PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts(); string echo = channel.Echo("hello"); echo = channel.Echo("hello"); PerCallInstanceContextSimpleServiceAndBehavior.WaitForDisposalCount(2, TimeSpan.FromSeconds(30)); Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount); Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount); ((System.ServiceModel.Channels.IChannel)channel).Close(); factory.Close(); TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory); }
public void SimpleNetTcpClientImpersonateUser() { string sourceString = "test"; var host = ServiceHelper.CreateWebHostBuilder <ImpersonateCallerForAll>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null; ClientContract.ITestService channel = null; host.Start(); try { var binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport); factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding, new System.ServiceModel.EndpointAddress(new Uri(WindowsAuthNetTcpServiceUri))); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var result = channel.EchoForImpersonation(sourceString); Assert.Equal(sourceString, result); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
public void WSHttpRequestReplyWithTransportMessageEchoStringDemuxFailure() { string testString = new string('a', 3000); IWebHost host = ServiceHelper.CreateHttpsWebHostBuilder <WSHttpTransportWithMessageCredentialWithUserNameExpire>(_output).Build(); using (host) { host.Start(); System.ServiceModel.WSHttpBinding wsHttpBinding = ClientHelper.GetBufferedModeWSHttpBinding(System.ServiceModel.SecurityMode.TransportWithMessageCredential); wsHttpBinding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName; var factory = new System.ServiceModel.ChannelFactory <ClientContract.IEchoService>(wsHttpBinding, new System.ServiceModel.EndpointAddress(new Uri("https://localhost:8443/WSHttpWcfService/basichttp.svc"))); ClientCredentials clientCredentials = (ClientCredentials)factory.Endpoint.EndpointBehaviors[typeof(ClientCredentials)]; clientCredentials.UserName.UserName = "******"; clientCredentials.UserName.Password = "******"; factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication { CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None }; ClientContract.IEchoService channel = factory.CreateChannel(); ((IChannel)channel).Open(); Thread.Sleep(6000); try { channel.EchoString(testString); }catch (Exception ex) { Assert.True(typeof(System.ServiceModel.FaultException).Equals(ex.InnerException.GetType())); Assert.Contains("expired security context token", ex.InnerException.Message); } } }
static void Main(string[] args) { Console.WriteLine("start service..."); Console.WriteLine("Open Service"); WcfRevitLibrary.IRevitExternalService service; try { System.ServiceModel.ChannelFactory <WcfRevitLibrary.IRevitExternalService> channelFactory = new System.ServiceModel.ChannelFactory <WcfRevitLibrary.IRevitExternalService>("IRevitExternalService"); service = channelFactory.CreateChannel(); } catch (Exception ex) { Console.WriteLine(ex); Console.ReadLine(); return; } Console.WriteLine("ready to load family"); bool isLoad = service.LoadFamily(@"C:\Users\xu.lanhui\Desktop\二次开发\矩形柱帽 斜角 -wcf.rfa"); Console.WriteLine(isLoad); Console.ReadLine(); }
public IList<Contracts.Entities.Recipe> GetRecentRecipes(int maxcount) { //TODO: call actual WCF service and make call using (System.ServiceModel.ChannelFactory<IRecipeService> factory = new System.ServiceModel.ChannelFactory<IRecipeService>("RecipeServiceHTTP")) { IRecipeService service = factory.CreateChannel(); return service.GetRecentRecipes(maxcount); } }
public void InitializeTest() { System.Threading.Monitor.Enter(mobileStagingServiceProxyTable); try { mobileStagingServiceProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out mobileStagingServiceClient); if (((mobileStagingServiceClient == null) || (((System.ServiceModel.ICommunicationObject)(mobileStagingServiceClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory<on.the.dot.IMobileStagingService> mobileStagingServiceFactory = new System.ServiceModel.ChannelFactory<on.the.dot.IMobileStagingService>("BasicHttpBinding_IMobileStagingService"); mobileStagingServiceClient = mobileStagingServiceFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(mobileStagingServiceClient)).Open(); mobileStagingServiceProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = mobileStagingServiceClient; } } finally { System.Threading.Monitor.Exit(mobileStagingServiceProxyTable); } }
static void call() { using (var f = new System.ServiceModel.ChannelFactory<IServiceT>(new System.ServiceModel.NetTcpBinding(), addr)) { IServiceT proxy = f.CreateChannel(); WriteLine(proxy.gettime(1)); } }
//구성정보 없이 코드로 Endpoint 구성후 호출 private void CallServiceWithoutConfig() { try { // 1. 클라이언트 상에 서비스 endpoint 정의하기 System.ServiceModel.Description.ServiceEndpoint httpEndpoint = new System.ServiceModel.Description.ServiceEndpoint( System.ServiceModel.Description.ContractDescription.GetContract( typeof(IEchoService)),new System.ServiceModel.WSHttpBinding(), new System.ServiceModel.EndpointAddress("http://xdn-dcpark/Server_Console/EchoService")); System.ServiceModel.Description.ServiceEndpoint tcpEndpoint= new System.ServiceModel.Description.ServiceEndpoint( System.ServiceModel.Description.ContractDescription.GetContract( typeof(IEchoService)), new System.ServiceModel.NetTcpBinding(), new System.ServiceModel.EndpointAddress("net.tcp://xdn-dcpark:20000/Server_Console/EchoService")); System.ServiceModel.Description.ServiceEndpoint namedPipeEndpoint= new System.ServiceModel.Description.ServiceEndpoint( System.ServiceModel.Description.ContractDescription.GetContract( typeof(IEchoService)), new System.ServiceModel.NetNamedPipeBinding(), new System.ServiceModel.EndpointAddress("net.pipe://xdn-dcpark/Server_Console/EchoService")); IEchoService svc = null; // 2. Endpoint 에 기반한 Channel Factory 생성 using (System.ServiceModel.ChannelFactory<IEchoService> httpFactory = new System.ServiceModel.ChannelFactory<IEchoService>(httpEndpoint)) { // 3. Endpoint에 대한 Chennel proxy 생성 svc = httpFactory.CreateChannel(); // 4. 서비스 오퍼레이션 호출 this.txtStatus.Text += String.Format("Invoking HTTP endpoint(ONLY CODE): {0}\r\n", svc.Echo("Duck Chang Park")); } // 2. Endpoint 에 기반한 Channel Factory 생성 using (System.ServiceModel.ChannelFactory<IEchoService> tcpFactory = new System.ServiceModel.ChannelFactory<IEchoService>(tcpEndpoint)) { // 3. Endpoint에 대한 Chennel proxy 생성 svc = tcpFactory.CreateChannel(); // 4. 서비스 오퍼레이션 호출 this.txtStatus.Text += String.Format("Invoking TCP endpoint(ONLY CODE): {0}\r\n", svc.Echo("Duck Chang Park")); } // 2. Endpoint 에 기반한 Channel Factory 생성 using (System.ServiceModel.ChannelFactory<IEchoService> namedPipeFactory = new System.ServiceModel.ChannelFactory<IEchoService>(namedPipeEndpoint)) { // 3. Endpoint에 대한 Chennel proxy 생성 svc = namedPipeFactory.CreateChannel(); // 4. 서비스 오퍼레이션 호출 this.txtStatus.Text += String.Format("Invoking NAMED PIPE endpoint(ONLY CODE): {0}\r\n", svc.Echo("Duck Chang Park")); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { this.txtStatus.Text += String.Format("--------------------------------------\r\n"); } }