本文整理汇总了C#中ChannelFactory.CreateChannel方法的典型用法代码示例。如果您正苦于以下问题:C# ChannelFactory.CreateChannel方法的具体用法?C# ChannelFactory.CreateChannel怎么用?C# ChannelFactory.CreateChannel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ChannelFactory
的用法示例。
在下文中一共展示了ChannelFactory.CreateChannel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ServerAncClientExceptionsEndpointBehavior
public void ServerAncClientExceptionsEndpointBehavior()
{
var hook = new ExceptionsEndpointBehaviour();
var address = @"net.pipe://127.0.0.1/test" + this.GetType().Name + "_" + MethodBase.GetCurrentMethod().Name;
var serv = new ExceptionService();
using (var host = new ServiceHost(serv, new Uri[] { new Uri(address), }))
{
var b = new NetNamedPipeBinding();
var serverEndpoint = host.AddServiceEndpoint(typeof(IExceptionService), b, address);
serverEndpoint.Behaviors.Add(hook);
host.Open();
var f = new ChannelFactory<IExceptionService>(b);
f.Endpoint.Behaviors.Add(hook);
var c = f.CreateChannel(new EndpointAddress(address));
try
{
c.DoException("message");
}
catch (InvalidOperationException ex)
{
StringAssert.AreEqualIgnoringCase("message", ex.Message);
}
host.Abort();
}
}
示例2: DefaultSettings_Echo_RoundTrips_String
public static void DefaultSettings_Echo_RoundTrips_String()
{
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;
string testString = "Hello";
Binding binding = null;
try
{
// *** SETUP *** \\
binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic));
serviceProxy = factory.CreateChannel();
// *** EXECUTE *** \\
string result = serviceProxy.Echo(testString);
// *** VALIDATE *** \\
Assert.True(result == testString, String.Format("Error: expected response from service: '{0}' Actual was: '{1}'", testString, result));
// *** CLEANUP *** \\
factory.Close();
((ICommunicationObject)serviceProxy).Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}
示例3: SameBinding_Soap11_EchoString
public static void SameBinding_Soap11_EchoString()
{
string variationDetails = "Client:: CustomBinding/HttpTransport/TextEncoding/Soap11 = None\nServer:: CustomBinding/HttpTransport/TextEncoding/Soap11";
string testString = "Hello";
StringBuilder errorBuilder = new StringBuilder();
bool success = false;
try
{
CustomBinding binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8), new HttpTransportBindingElement());
ChannelFactory<IWcfService> factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.HttpSoap11_Address));
IWcfService serviceProxy = factory.CreateChannel();
string result = serviceProxy.Echo(testString);
success = string.Equals(result, testString);
if (!success)
{
errorBuilder.AppendLine(String.Format(" Error: expected response from service: '{0}' Actual was: '{1}'", testString, result));
}
}
catch (Exception ex)
{
errorBuilder.AppendLine(String.Format(" Error: Unexpected exception was caught while doing the basic echo test for variation...\n'{0}'\nException: {1}", variationDetails, ex.ToString()));
for (Exception innerException = ex.InnerException; innerException != null; innerException = innerException.InnerException)
{
errorBuilder.AppendLine(String.Format("Inner exception: {0}", innerException.ToString()));
}
}
Assert.True(errorBuilder.Length == 0, "Test case FAILED with errors: " + errorBuilder.ToString());
}
示例4: Ipc_byteArray
public void Ipc_byteArray()
{
var uri = "ipc:///" + MethodBase.GetCurrentMethod().Name;
using (var server = new ServiceHost(new Service(), new Uri(uri)))
{
var binding = new LocalBinding { MaxConnections = 5 };
server.AddServiceEndpoint(typeof(IService), binding, uri);
server.Open();
Thread.Sleep(100);
using (var channelFactory = new ChannelFactory<IService>(binding))
{
var client = channelFactory.CreateChannel(new EndpointAddress(uri));
client.Execute(new byte[0]);
byte[] bytes = new byte[512];
new Random().NextBytes(bytes);
var timer = new Stopwatch();
timer.Start();
for (int i = 0; i < 5000; i++)
client.Execute(bytes);
timer.Stop();
Trace.WriteLine(timer.ElapsedMilliseconds.ToString() + " ms", MethodBase.GetCurrentMethod().Name);
}
}
}
示例5: BasicAuthenticationInvalidPwd_throw_MessageSecurityException
public static void BasicAuthenticationInvalidPwd_throw_MessageSecurityException()
{
StringBuilder errorBuilder = new StringBuilder();
// Will need to use localized string once it is available
// On Native retail, the message is stripped to 'HttpAuthorizationForbidden, Basic'
// On Debug or .Net Core, the entire message is "The HTTP request was forbidden with client authentication scheme 'Basic'."
// Thus we will only check message contains "forbidden"
string message = "forbidden";
MessageSecurityException exception = Assert.Throws<MessageSecurityException>(() =>
{
BasicHttpBinding basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
ChannelFactory<IWcfCustomUserNameService> factory = new ChannelFactory<IWcfCustomUserNameService>(basicHttpBinding, new EndpointAddress(Endpoints.Https_BasicAuth_Address));
factory.Credentials.UserName.UserName = "test1";
factory.Credentials.UserName.Password = "test1";
IWcfCustomUserNameService serviceProxy = factory.CreateChannel();
string testString = "I am a test";
string result = serviceProxy.Echo(testString);
});
Assert.True(exception.Message.ToLower().Contains(message), string.Format("Expected exception message to contain: '{0}', actual message is: '{1}'", message, exception.Message));
}
示例6: CustomBinding_Message_Interceptor
public static void CustomBinding_Message_Interceptor()
{
ChannelFactory<IWcfChannelExtensibilityContract> factory = null;
IWcfChannelExtensibilityContract serviceProxy = null;
try
{
// *** SETUP *** \\
CustomBinding binding = new CustomBinding(
new InterceptingBindingElement(new MessageModifier()),
new HttpTransportBindingElement());
factory = new ChannelFactory<IWcfChannelExtensibilityContract>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_ChannelExtensibility));
serviceProxy = factory.CreateChannel();
// *** EXECUTE & VALIDATE *** \\
int[] windSpeeds = new int[] { 100, 90, 80, 70, 60, 50, 40, 30, 20, 10 };
for (int i = 0; i < 10; i++)
{
serviceProxy.ReportWindSpeed(windSpeeds[i]);
}
// *** CLEANUP *** \\
((ICommunicationObject)serviceProxy).Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}
示例7: UnexpectedException_Throws_FaultException
public static void UnexpectedException_Throws_FaultException()
{
string faultMsg = "This is a test fault msg";
BasicHttpBinding binding = null;
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;
EndpointAddress endpointAddress = null;
FaultException<ExceptionDetail> exception = Assert.Throws<FaultException<ExceptionDetail>>(() =>
{
// *** SETUP *** \\
binding = new BasicHttpBinding();
endpointAddress = new EndpointAddress(Endpoints.HttpBaseAddress_Basic);
factory = new ChannelFactory<IWcfService>(binding, endpointAddress);
serviceProxy = factory.CreateChannel();
// *** EXECUTE *** \\
try
{
serviceProxy.ThrowInvalidOperationException(faultMsg);
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
});
// *** ADDITIONAL VALIDATION *** \\
Assert.True(String.Equals(exception.Detail.Message, faultMsg), String.Format("Expected Fault Message: {0}, actual: {1}", faultMsg, exception.Detail.Message));
}
示例8: NonExistentAction_Throws_ActionNotSupportedException
public static void NonExistentAction_Throws_ActionNotSupportedException()
{
string exceptionMsg = "The message with Action 'http://tempuri.org/IWcfService/NotExistOnServer' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).";
try
{
BasicHttpBinding binding = new BasicHttpBinding();
using (ChannelFactory<IWcfService> factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic)))
{
IWcfService serviceProxy = factory.CreateChannel();
serviceProxy.NotExistOnServer();
}
}
catch (Exception e)
{
if (e.GetType() != typeof(System.ServiceModel.ActionNotSupportedException))
{
Assert.True(false, string.Format("Expected exception: {0}, actual: {1}", "ActionNotSupportedException", e.GetType()));
}
if (e.Message != exceptionMsg)
{
Assert.True(false, string.Format("Expected Fault Message: {0}, actual: {1}", exceptionMsg, e.Message));
}
return;
}
Assert.True(false, "Expected ActionNotSupportedException exception, but no exception thrown.");
}
示例9: SendTimeout_For_Long_Running_Operation_Throws_TimeoutException
public static void SendTimeout_For_Long_Running_Operation_Throws_TimeoutException()
{
TimeSpan serviceOperationTimeout = TimeSpan.FromMilliseconds(10000);
BasicHttpBinding binding = new BasicHttpBinding();
binding.SendTimeout = TimeSpan.FromMilliseconds(5000);
ChannelFactory<IWcfService> factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic));
Stopwatch watch = new Stopwatch();
try
{
var exception = Assert.Throws<TimeoutException>(() =>
{
IWcfService proxy = factory.CreateChannel();
watch.Start();
proxy.EchoWithTimeout("Hello", serviceOperationTimeout);
});
}
finally
{
watch.Stop();
}
// want to assert that this completed in > 5 s as an upper bound since the SendTimeout is 5 sec
// (usual case is around 5001-5005 ms)
Assert.InRange<long>(watch.ElapsedMilliseconds, 4985, 6000);
}
示例10: SameBinding_SecurityModeNone_EchoString
public static void SameBinding_SecurityModeNone_EchoString()
{
string variationDetails = "Client:: NetTcpBinding/SecurityMode = None\nServer:: NetTcpBinding/SecurityMode = None";
string testString = "Hello";
StringBuilder errorBuilder = new StringBuilder();
bool success = false;
try
{
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
ChannelFactory<IWcfService> factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
IWcfService serviceProxy = factory.CreateChannel();
string result = serviceProxy.Echo(testString);
success = string.Equals(result, testString);
if (!success)
{
errorBuilder.AppendLine(String.Format(" Error: expected response from service: '{0}' Actual was: '{1}'", testString, result));
}
}
catch (Exception ex)
{
errorBuilder.AppendLine(String.Format(" Error: Unexpected exception was caught while doing the basic echo test for variation...\n'{0}'\nException: {1}", variationDetails, ex.ToString()));
for (Exception innerException = ex.InnerException; innerException != null; innerException = innerException.InnerException)
{
errorBuilder.AppendLine(String.Format("Inner exception: {0}", innerException.ToString()));
}
}
Assert.True(errorBuilder.Length == 0, "Test case FAILED with errors: " + errorBuilder.ToString());
}
示例11: NetTcp_TransportSecurity_StreamedResponse_RoundTrips_String
public static void NetTcp_TransportSecurity_StreamedResponse_RoundTrips_String()
{
string testString = "Hello";
NetTcpBinding binding = null;
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;
try
{
// *** SETUP *** \\
binding = new NetTcpBinding(SecurityMode.Transport);
binding.TransferMode = TransferMode.StreamedResponse;
factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.Tcp_Transport_Security_Streamed_Address));
serviceProxy = factory.CreateChannel();
// *** EXECUTE *** \\
var returnStream = serviceProxy.GetStreamFromString(testString);
var result = StreamToString(returnStream);
// *** VALIDATE *** \\
Assert.Equal(testString, result);
// *** CLEANUP *** \\
((ICommunicationObject)serviceProxy).Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}
示例12: NotExistentHost_Throws_EndpointNotFoundException
public static void NotExistentHost_Throws_EndpointNotFoundException()
{
string nonExistentHost = "http://nonexisthost/WcfService/WindowsCommunicationFoundation";
ChannelFactory<IWcfService> factory = null;
EndpointAddress endpointAddress = null;
BasicHttpBinding binding = null;
IWcfService serviceProxy = null;
// *** VALIDATE *** \\
EndpointNotFoundException exception = Assert.Throws<EndpointNotFoundException>(() =>
{
// *** SETUP *** \\
binding = new BasicHttpBinding();
binding.SendTimeout = TimeSpan.FromMilliseconds(20000);
endpointAddress = new EndpointAddress(nonExistentHost);
factory = new ChannelFactory<IWcfService>(binding, endpointAddress);
serviceProxy = factory.CreateChannel();
// *** EXECUTE *** \\
try
{
serviceProxy.Echo("Hello");
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
});
// *** ADDITIONAL VALIDATION FOR NET NATIVE *** \\
// On .Net Native retail, exception message is stripped to include only parameter
Assert.True(exception.Message.Contains(nonExistentHost), string.Format("Expected exception message to contain: '{0}'\nThe exception message was: {1}", nonExistentHost, exception.Message));
}
示例13: CallServiceReturningSession2TimesFor2Channels_sessionAreDifferentForDifferentChannels
public void CallServiceReturningSession2TimesFor2Channels_sessionAreDifferentForDifferentChannels()
{
var address = @"net.pipe://127.0.0.1/1/test.test/test" + MethodBase.GetCurrentMethod().Name;
var serv = new SessionService();
var host = new ServiceHost(serv, new Uri(address));
var b = new NetNamedPipeBinding();
host.AddServiceEndpoint(typeof(ISessionService), b, address);
var f1 = new ChannelFactory<ISessionService>(b);
var f2 = new ChannelFactory<ISessionService>(b);
var client1 = f1.CreateChannel(new EndpointAddress(address));
var client2 = f2.CreateChannel(new EndpointAddress(address));
host.Open();
var session11 = client1.Call();
var session21 = client2.Call();
var session22 = client2.Call();
var session12 = client1.Call();
f1.Dispose();
f2.Dispose();
host.Dispose();
Assert.AreEqual(session11, session12);
Assert.AreEqual(session21, session22);
Assert.AreNotEqual(session11, session21);
}
示例14: SameBinding_Soap11_EchoString
public static void SameBinding_Soap11_EchoString()
{
CustomBinding binding = null;
EndpointAddress endpointAddress = null;
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;
string testString = "Hello";
string result = null;
try
{
// *** SETUP *** \\
binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8), new HttpTransportBindingElement());
endpointAddress = new EndpointAddress(Endpoints.HttpSoap11_Address);
factory = new ChannelFactory<IWcfService>(binding, endpointAddress);
serviceProxy = factory.CreateChannel();
// *** EXECUTE *** \\
result = serviceProxy.Echo(testString);
// *** VALIDATE *** \\
Assert.True(String.Equals(result, testString), String.Format(" Error: expected response from service: '{0}' Actual was: '{1}'", testString, result));
// *** CLEANUP *** \\
factory.Close();
((ICommunicationObject)serviceProxy).Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}
示例15: SecurityModeTransport_Echo_RoundTrips_String
public static void SecurityModeTransport_Echo_RoundTrips_String()
{
string testString = "Hello";
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;
try
{
// *** SETUP *** \\
NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);
factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.Tcp_DefaultBinding_Address));
serviceProxy = factory.CreateChannel();
// *** EXECUTE *** \\
string result = serviceProxy.Echo(testString);
// *** VALIDATE *** \\
Assert.Equal(testString, result);
// *** CLEANUP *** \\
((ICommunicationObject)serviceProxy).Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}