我是否需要传入URL来调用WCF Web服务?

问题描述 投票:0回答:1

我正在将我的客户端Web服务从过时的ASP.NET ASMX和Microsoft.NET WSE库更新到更新的WCF实现。但我没有足够的文档知道我是否需要传递如下代码的网址?

在旧样式中,从Web服务库中的某个位置观察到url,在新调用中,我必须在url中输入以连接并创建通道工厂。我只是想知道这个网址是否可以像旧式一样被绕过?

这是旧代码

_TrustCurrentWebServices = new WebServiceAWI();
_User = new User();
_User.UserName = _TrustCurrentWebServices.EncryptValue(_BPWebServicesUserName);
_User.Password = _TrustCurrentWebServices.EncryptValue(_BPWebServicesPassword);

Hre是新代码。我不想传递网址!

var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport); 
var endpoint = new EndpointAddress(_Url);
var channelFactory = new ChannelFactory<WebServiceAWI>(binding, endpoint);
_WebServiceAWI = channelFactory.CreateChannel();

_User = new User();
_User.UserName = _WebServiceAWI.EncryptValue(_BPWebServicesUserName);
_User.Password = _WebServiceAWI.EncryptValue(_BPWebServicesPassword);

我试过了

var channelFactory = new ChannelFactory<WebServiceAWI>(binding);

但收到一个例外,说我需要一个端点地址!

c# asp.net wcf asmx
1个回答
0
投票

通过使用ChannelFactory创建频道来调用服务时,您可以参考以下代码片段来设置客户端凭据。

Server end(10.157.13.69, Console app)
        static void Main(string[] args)
        {
            Uri uri = new Uri("https://localhost:11011");
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

            using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
            {
                sh.AddServiceEndpoint(typeof(IService), binding, "");
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior()
                    {
                        HttpsGetEnabled = true
                    };
                    sh.Description.Behaviors.Add(smb);
                }
                sh.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
                sh.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustUserNamePasswordVal();
                Binding mexbinding = MetadataExchangeBindings.CreateMexHttpsBinding();
                sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");

                sh.Opened += delegate
                {
                    Console.WriteLine("Service is ready");
                };
                sh.Closed += delegate
                {
                    Console.WriteLine("Service is clsoed");
                };


                sh.Open();

                Console.ReadLine();
                sh.Close();
                Console.ReadLine();
            }

        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string SayHello();
    }
    public class MyService : IService
    {
        public string SayHello()
        {
            return $"Hello Stranger,{DateTime.Now.ToLongTimeString()}";
        }
    }
    internal class CustUserNamePasswordVal : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (userName != "jack" || password != "123456")
            {
                throw new Exception("Username/Password is not correct");
            }
        }
}

客户端调用。

ServicePointManager.ServerCertificateValidationCallback += delegate
            {
                return true;
            };
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress("https://10.157.13.69:11011"));
            factory.Credentials.UserName.UserName = "jack";
            factory.Credentials.UserName.Password = "123456";
            IService sv = factory.CreateChannel();
            try
            {
                var result = sv.SayHello();
                Console.WriteLine(result);
            }
            catch (Exception)
            {

                throw;
            }

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channelfactory-1?redirectedfrom=MSDN&view=netframework-4.7.2

如果有什么我可以帮忙,请随时告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.