如何将参数发送到只接受C#中的一个参数的webservice方法

时间:2015-05-06 12:04:16

标签: c# web-services asmx

我是webservices的新手。我正在尝试向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);

1 个答案:

答案 0 :(得分:3)

试试这个:

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