简体   繁体   English

如何将参数发送到仅接受C#中的一个参数的Web服务方法

[英]How to send parameters to a webservice method that only accepts one argument in C#

I am new to webservices in general. 我是Web服务的新手。 I am trying to add 4 parameters to the rmc service CreateCar, and have attempted to do so in my Webservices.cs file. 我正在尝试向rmc服务CreateCar添加4个参数,并试图在我的Webservices.cs文件中这样做。 Unfortunately, the CreateCar method only takes 1 argument (request). 不幸的是,CreateCar方法仅接受1个参数(请求)。 Most of this code is based on similar processes that do accept all the parameters. 大部分代码基于接受所有参数的相似过程。

The call I am trying to make is the rmcService.CreateCar. 我要拨打的电话是rmcService.CreateCar。

    public bool CreateCar(string url, string viewedUserType, string userName, string accounts, string carName)
    {
        bool returnValue = false;

        try
        {
            if (accounts.Length != 9)
            {
                if (accounts.Length == 8)
                {
                    accounts = accounts + "0";
                }
                else
                {
                    throw new ApplicationException("Invalid length for Account #");
                }
            }

            // Initialize web service object
            relationshipmgmtcentersvcdev.RmcService rmcService = new relationshipmgmtcentersvcdev.RmcService();
            rmcService.Url = url;

            // Attach credentials
            rmcService.Credentials = _Credentials;

            // Connection pooling
            rmcService.ConnectionGroupName = _Credentials.UserName.ToString();
            rmcService.UnsafeAuthenticatedConnectionSharing = true;

            // Hard coding View User Type as None
            viewedUserType = "None";

            // Call to create CAR - Client Account Relation
            rmcService.CreateCar(userName, viewedUserType, accounts, carName); 
            returnValue = true;
        }
        catch (Exception ex)
        {
            System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog("Application");
            eventLog.Source = "UAccCompWrapper";
            eventLog.WriteEntry("Error in CreateCar Method: " + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
        }

        return returnValue;
    }

The error I receive is no overload for method CreateCar takes 4 arguments (which makes sense). 我收到的错误是方法CreateCar的任何重载都没有4个参数(这很有意义)。 I was just trying to illustrate what I was trying to do. 我只是想举例说明我要做什么。 This method only takes 1 argument it seems. 这种方法似乎只有1个参数。

This is a snippet of the reference file with the information I found was relevant. 这是参考文件的片段,其中包含我发现的相关信息。 It appears that I need to create a BaseServiceRequest with the parameters, but I am unsure how. 看来我需要使用参数创建BaseServiceRequest,但不确定如何。

  /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://relationshipmgmtcenter.hfg.com/services/IRmcService/CreateCar", RequestNamespace="http://relationshipmgmtcenter.hfg.com/services/", ResponseNamespace="http://relationshipmgmtcenter.hfg.com/services/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public void CreateCar([System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] CreateCarServiceRequest request) {
        this.Invoke("CreateCar", new object[] {
                    request});
    }

    /// <remarks/>
    public void CreateCarAsync(CreateCarServiceRequest request) {
        this.CreateCarAsync(request, null);
    }

    /// <remarks/>
    public void CreateCarAsync(CreateCarServiceRequest request, object userState) {
        if ((this.CreateCarOperationCompleted == null)) {
            this.CreateCarOperationCompleted = new System.Threading.SendOrPostCallback(this.OnCreateCarOperationCompleted);
        }
        this.InvokeAsync("CreateCar", new object[] {
                    request}, this.CreateCarOperationCompleted, userState);
    }

    private void OnCreateCarOperationCompleted(object arg) {
        if ((this.CreateCarCompleted != null)) {
            System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
            this.CreateCarCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
        }
    }


/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(UserProfileServiceRequest))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(RenameGroupServiceRequest))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(CreateCarServiceRequest))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DeleteGroupServiceRequest))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(GetMgrServiceRequest))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/HF.Rmc.Service.Library.Requests")]
public partial class BaseServiceRequest {

    private string sessionIdField;

    private string userNameField;

    private string viewedUserField;

    private ViewedUserType viewedUserTypeField;

    private bool viewedUserTypeFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string SessionId {
        get {
            return this.sessionIdField;
        }
        set {
            this.sessionIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string UserName {
        get {
            return this.userNameField;
        }
        set {
            this.userNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string ViewedUser {
        get {
            return this.viewedUserField;
        }
        set {
            this.viewedUserField = value;
        }
    }

    /// <remarks/>
    public ViewedUserType ViewedUserType {
        get {
            return this.viewedUserTypeField;
        }
        set {
            this.viewedUserTypeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ViewedUserTypeSpecified {
        get {
            return this.viewedUserTypeFieldSpecified;
        }
        set {
            this.viewedUserTypeFieldSpecified = value;
        }
    }
}


public partial class CreateCarServiceRequest : BaseServiceRequest {

    private string accountsField;

    private string carNameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string Accounts {
        get {
            return this.accountsField;
        }
        set {
            this.accountsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string CarName {
        get {
            return this.carNameField;
        }
        set {
            this.carNameField = value;
        }
    }
} 
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.18408")]
public delegate void CreateCarCompletedEventHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e);

Try this: 尝试这个:

rmcService.CreateCar(new CreateCarServiceRequest
                    {
                        UserName = userName,
                        ViewedUserType = Enum.Parse(typeof(ViewedUserType), viewedUserType)
                        Accounts = accounts,
                        CarName = carName
                    });  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM