测试WCF服务和客户端

时间:2012-12-06 00:12:29

标签: wcf

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

ConnectClientToTestService()抛出错误:

  

System.ServiceModel.Security.SecurityNegotiationException:安全   由于与远程安全协商,无法打开通道   端点失败了。这可能是由于缺席或不正确   用于创建的EndpointAddress中指定的EndpointIdentity   渠道。请验证指定或暗示的EndpointIdentity   EndpointAddress正确标识远程端点。 --->   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();
                }
            }
        }
    }
}

0 个答案:

没有答案