【问题标题】:Do I need to pass in the url to call a WCF web service?我是否需要传入 url 才能调用 WCF Web 服务?
【发布时间】:2019-08-07 00:46:33
【问题描述】:

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

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

这是旧代码

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

这里是新代码。我不想传入url!

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】:

    使用ChannelFactory创建通道调用服务时,可以参考以下代码sn-ps设置客户端凭据。

    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

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

    【讨论】:

      猜你喜欢
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2013-10-16
      • 2017-03-13
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多