如何将参数发送到仅接受 C# 中的一个参数的 webservice 方法
How to send parameters to a webservice method that only accepts one argument in C#
总的来说,我是网络服务的新手。我正在尝试向 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
});
总的来说,我是网络服务的新手。我正在尝试向 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
});