簡體   English   中英

如何將參數發送到僅接受C#中的一個參數的Web服務方法

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

我是Web服務的新手。 我正在嘗試向rmc服務CreateCar添加4個參數,並試圖在我的Webservices.cs文件中這樣做。 不幸的是,CreateCar方法僅接受1個參數(請求)。 大部分代碼基於接受所有參數的相似過程。

我要撥打的電話是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;
    }

我收到的錯誤是方法CreateCar的任何重載都沒有4個參數(這很有意義)。 我只是想舉例說明我要做什么。 這種方法似乎只有1個參數。

這是參考文件的片段,其中包含我發現的相關信息。 看來我需要使用參數創建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);

嘗試這個:

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