2

我正在尝试编写一个运行该服务然后将客户端连接到该服务的集成测试。

ConnectClientToTestService() 抛出错误:

System.ServiceModel.Security.SecurityNegotiationException:无法打开安全通道,因为与远程端点的安全协商失败。这可能是由于用于创建通道的 EndpointAddress 中缺少或错误指定了 EndpointIdentity。请验证 EndpointAddress 指定或暗示的 EndpointIdentity 是否正确标识了远程端点。---> System.ServiceModel.FaultException:安全令牌请求包含无效或格式错误的元素。

你能在同一个exe中做到这一点吗?我的机器上安装了涉及的证书,但这些也可能是问题所在。

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();
                }
            }
        }
    }
}
4

0 回答 0