测试WCF服务和客户端
我正在尝试编写一个集成测试来运行该服务,然后将客户机连接到此服务 ConnectClientToTestService()引发错误: System.ServiceModel.Security.SecurityNegotiationException:安全 无法打开通道,因为正在与远程服务器进行安全协商 终结点已失败。这可能是由于缺席或错误 用于创建的EndpointAddress中指定的EndpointIdentity 频道请验证指定或暗示的EndpointIdentity EndpointAddress正确标识远程端点。--> System.ServiceModel.FaultException:对安全令牌的请求已失败 无效或格式错误的元素 你能在同一个exe中执行此操作吗?我的机器上安装了一些证书,但这些证书也可能是问题所在测试WCF服务和客户端,wcf,Wcf,我正在尝试编写一个集成测试来运行该服务,然后将客户机连接到此服务 ConnectClientToTestService()引发错误: System.ServiceModel.Security.SecurityNegotiationException:安全 无法打开通道,因为正在与远程服务器进行安全协商 终结点已失败。这可能是由于缺席或错误 用于创建的EndpointAddress中指定的EndpointIdentity 频道请验证指定或暗示的EndpointIdentity EndpointAd
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ServiceModel;
using ECS.Services;
using ECS.App.Core.ECSDataService;
using System.ServiceModel.Description;
namespace ECS.Test.ClientSide
{
[TestClass]
public class TestValidUserIntegrationTest
{
private static TestContext context;
[ClassInitialize()]
public static void ClassInitialize(TestContext testContext)
{
context = testContext;
ResourcingServiceHost.StartService();
}
/// <summary>
/// Shut down the WCF service once all tests have been run
/// </summary>
[ClassCleanup()]
public static void MyClassCleanup()
{
ResourcingServiceHost.StopService();
}
//Point the client at the test ResourceingServiceHost service
[TestMethod]
public void ConnectClientToTestService()
{
WSHttpBinding myBinding = new WSHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("http://localhost:8733/ECS.Services/DataService/");
var factory = new ChannelFactory<ECS.App.Core.ECSDataService.IDataService>("debug", new EndpointAddress("http://localhost:8733/ECS.Services/DataService/"));//new ChannelFactory<ECS.App.Core.ECSDataService.IDataService>(myBinding, myEndpoint);//
{
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "admin";
clientCredentials.UserName.Password = "a";
factory.Endpoint.Behaviors.RemoveAll<ClientCredentials>();
factory.Endpoint.Behaviors.Add(clientCredentials);
ECS.App.Core.ECSDataService.IDataService client = factory.CreateChannel();
using (Channel.AsDisposable(client))
{
client.GetConnectionStrings();
}
}
}
}
internal class ResourcingServiceHost
{
internal static ServiceHost Instance = null;
internal static void StartService()
{
Instance = new ServiceHost(typeof(DataService));
WSHttpBinding wsBinding = new WSHttpBinding();
wsBinding.Security.Mode = SecurityMode.Message;
wsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
Instance.AddServiceEndpoint(typeof(ECS.Services.IDataService), wsBinding, "http://localhost:8733/ECS.Services/DataService/");
Instance.Open();
}
internal static void StopService()
{
if (Instance.State != CommunicationState.Closed)
{
Instance.Close();
}
}
}
//This allows us to see the inner exceptions from the WCF service
public class Channel : IDisposable
{
private ICommunicationObject _channel;
private Channel(ICommunicationObject channel)
{
_channel = channel;
}
public static IDisposable AsDisposable(object client)
{
return new Channel((ICommunicationObject)client);
}
public void Dispose()
{
bool success = false;
try
{
if (_channel.State != CommunicationState.Faulted)
{
_channel.Close(); success = true;
}
}
finally
{
if (!success)
{
_channel.Abort();
}
}
}
}
}
使用系统;
使用系统文本;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用System.ServiceModel;
使用云服务器服务;
使用ECS.App.Core.ECSDataService;
使用System.ServiceModel.Description;
命名空间ECS.Test.ClientSide
{
[测试类]
公共类TestValidUserIntegrationTest
{
私有静态测试上下文;
[ClassInitialize()]
公共静态void ClassInitialize(TestContext TestContext)
{
context=testContext;
ResourceingServiceHost.StartService();
}
///
///运行所有测试后关闭WCF服务
///
[ClassCleanup()]
公共静态void MyClassCleanup()
{
ResourceingServiceHost.StopService();
}
//将客户端指向测试资源服务主机服务
[测试方法]
公共无效ConnectClientToTestService()
{
WSHttpBinding myBinding=新的WSHttpBinding();
EndpointAddress myEndpoint=新的EndpointAddress(“http://localhost:8733/ECS.Services/DataService/");
var factory=newchannelfactory(“调试”),newendpointaddress(“http://localhost:8733/ECS.Services/DataService/);//新的ChannelFactory(myBinding,myEndpoint)//
{
ClientCredentials ClientCredentials=新的ClientCredentials();
clientCredentials.UserName.UserName=“admin”;
clientCredentials.UserName.Password=“a”;
factory.Endpoint.Behaviors.RemoveAll();
factory.Endpoint.Behaviors.Add(clientCredentials);
ECS.App.Core.ECSDataService.IDataService client=factory.CreateChannel();
使用(Channel.AsDisposable(客户端))
{
client.getConnectionString();
}
}
}
}
内部类资源配置服务主机
{
内部静态ServiceHost实例=null;
内部静态void StartService()
{
实例=新的ServiceHost(typeof(DataService));
WSHttpBinding wsBinding=新的WSHttpBinding();
wsBinding.Security.Mode=SecurityMode.Message;
wsBinding.Security.Transport.ClientCredentialType=HttpClientCredentialType.Windows;
Instance.AddServiceEndpoint(typeof(ECS.Services.IDataService),wsBinding,“http://localhost:8733/ECS.Services/DataService/");
Open();
}
内部静态void StopService()
{
if(Instance.State!=CommunicationState.Closed)
{
实例Close();
}
}
}
//这允许我们查看WCF服务的内部异常
公共类频道:IDisposable
{
专用ICommunicationObject\u通道;
专用通道(ICommunicationObject通道)
{
_通道=通道;
}
公共静态IDisposable为一次性(对象客户端)
{
返回新通道((ICommunicationObject)客户端);
}
公共空间处置()
{
布尔成功=假;
尝试
{
如果(_channel.State!=CommunicationState.Faulted)
{
_channel.Close();success=true;
}
}
最后
{
如果(!成功)
{
_channel.Abort();
}
}
}
}
}