ASP.NET Core MVC:WCF 客户端不包含采用 0 个参数的构造函数

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

我有一个带有 WCF 的 ASP.NET Core MVC 项目。我使用 IIS 将 WCF 发布到远程服务器,并通过以下方式使用客户端:

右键单击“连接服务”> 管理连接服务> 添加新服务引用

我从 .svc 页面粘贴了 WSDL 的链接。我设置的唯一自定义设置是选中“生成同步操作”并单击“完成”。当我尝试初始化 WCF 客户端时,出现以下错误:

'BillingSVCClient' does not contain a constructor that takes 0 arguments

这是我目前的代码:

Reference.cs(由 Visual Studio 生成)

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace BillingSVC
{
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="BillingSVC.IBillingSVC")]
    public interface IBillingSVC
    {
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IBillingSVC/AddConnectionType", ReplyAction="http://tempuri.org/IBillingSVC/AddConnectionTypeResponse")]
        void AddConnectionType(string conType, int classificationID, string code);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IBillingSVC/AddConnectionType", ReplyAction="http://tempuri.org/IBillingSVC/AddConnectionTypeResponse")]
        System.Threading.Tasks.Task AddConnectionTypeAsync(string conType, int classificationID, string code);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IBillingSVC/UpdateConnectionType", ReplyAction="http://tempuri.org/IBillingSVC/UpdateConnectionTypeResponse")]
        void UpdateConnectionType(int conTypeID, string conType, int classificationID, string code);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IBillingSVC/UpdateConnectionType", ReplyAction="http://tempuri.org/IBillingSVC/UpdateConnectionTypeResponse")]
        System.Threading.Tasks.Task UpdateConnectionTypeAsync(int conTypeID, string conType, int classificationID, string code);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IBillingSVC/DeleteConnectionType", ReplyAction="http://tempuri.org/IBillingSVC/DeleteConnectionTypeResponse")]
        void DeleteConnectionType(int conTypeID);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IBillingSVC/DeleteConnectionType", ReplyAction="http://tempuri.org/IBillingSVC/DeleteConnectionTypeResponse")]
        System.Threading.Tasks.Task DeleteConnectionTypeAsync(int conTypeID);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
    public interface IBillingSVCChannel : BillingSVC.IBillingSVC, System.ServiceModel.IClientChannel
    {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
    public partial class BillingSVCClient : System.ServiceModel.ClientBase<BillingSVC.IBillingSVC>, BillingSVC.IBillingSVC
    {
        
        /// <summary>
        /// Implement this partial method to configure the service endpoint.
        /// </summary>
        /// <param name="serviceEndpoint">The endpoint to configure</param>
        /// <param name="clientCredentials">The client credentials</param>
        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
        
        public BillingSVCClient(EndpointConfiguration endpointConfiguration) : 
                base(BillingSVCClient.GetBindingForEndpoint(endpointConfiguration), BillingSVCClient.GetEndpointAddress(endpointConfiguration))
        {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public BillingSVCClient(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
                base(BillingSVCClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
        {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public BillingSVCClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(BillingSVCClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
        {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public BillingSVCClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }

        public void AddConnectionType(string conType, int classificationID, string code)
        {
            base.Channel.AddConnectionType(conType, classificationID, code);
        }
        
        public System.Threading.Tasks.Task AddConnectionTypeAsync(string conType, int classificationID, string code)
        {
            return base.Channel.AddConnectionTypeAsync(conType, classificationID, code);
        }
        
        public void UpdateConnectionType(int conTypeID, string conType, int classificationID, string code)
        {
            base.Channel.UpdateConnectionType(conTypeID, conType, classificationID, code);
        }
        
        public System.Threading.Tasks.Task UpdateConnectionTypeAsync(int conTypeID, string conType, int classificationID, string code)
        {
            return base.Channel.UpdateConnectionTypeAsync(conTypeID, conType, classificationID, code);
        }
        
        public void DeleteConnectionType(int conTypeID)
        {
            base.Channel.DeleteConnectionType(conTypeID);
        }
        
        public System.Threading.Tasks.Task DeleteConnectionTypeAsync(int conTypeID)
        {
            return base.Channel.DeleteConnectionTypeAsync(conTypeID);
        }
        
        public virtual System.Threading.Tasks.Task OpenAsync()
        {
            return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
        }
        
        private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IBillingSVC))
            {
                System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.AllowCookies = true;
                return result;
            }
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpsBinding_IBillingSVC))
            {
                System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.AllowCookies = true;
                result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
                return result;
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }
        
        private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IBillingSVC))
            {
                return new System.ServiceModel.EndpointAddress("http://192.168.2.160/CEDCBS_Overhaul_DevJD/BillingSVC.svc");
            }
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpsBinding_IBillingSVC))
            {
                return new System.ServiceModel.EndpointAddress("https://egstestdeploy/CEDCBS_Overhaul_DevJD/BillingSVC.svc");
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }
        
        public enum EndpointConfiguration
        {
            
            BasicHttpBinding_IBillingSVC,
            
            BasicHttpsBinding_IBillingSVC,
        }
    }
}

MyController.cs

public class Maintenance : Controller
{
    private readonly BillingSVCClient client = new BillingSVCClient(); // This is where the error occurs
    
    // Some IActionResult code here...
    
    [HttpPost]
    public async Task<IActionResult> AddConnectionType(string conType, int classificationID, string code) 
    {
        await client.OpenAsync();
        client.AddConnectionType(conType, classificationID, code);
        client.Close();
    
        return Ok();
    }
}

我习惯在旧的 ASP.NET MVC 项目上以这种方式初始化 WCF,但 ASP.NET Core MVC 似乎以不同的方式工作。任何帮助,将不胜感激。谢谢!

c# wcf asp.net-core-mvc wsdl
1个回答
0
投票

将代码添加到 Reference.cs

public BillingSVCClient() : 
         base(BillingSVCClient.GetDefaultBinding(), BillingSVCClient.GetDefaultEndpointAddress())
 {
     this.Endpoint.Name = endpointConfiguration.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
© www.soinside.com 2019 - 2024. All rights reserved.