客户端代理没有创建方法

本文关键字:创建 方法 代理 客户端 | 更新日期: 2023-09-27 18:08:46

有一个错误:Cannot implicitly convert type 'void' to 'string'。我按照正确创建webservice的每一步(首先编写服务,编译它并在浏览器中查看它)。然后使用"添加服务引用"向导创建服务引用)。以下是WP代码:

private void Button_Click(object sender, RoutedEventArgs e)
    {
         ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
        string myName = "Nick";
        AgeTxtBlck.Text = proxy.GetAgeAsync(myName);
    }

服务实现为:

public string GetAge(string myName)
    {
        DataClasses1DataContext data = new DataClasses1DataContext();
        var myAge = from a in data.Users where a.uName == myName select a;
        return GetAge(myName).ToString();
    }

让我的问题更清楚,这里是客户端生成的代理:

 public partial class Service1Client : System.ServiceModel.ClientBase<PhoneApp1.ServiceReference1.IService1>, PhoneApp1.ServiceReference1.IService1 {
    private BeginOperationDelegate onBeginGetAgeDelegate;
    private EndOperationDelegate onEndGetAgeDelegate;
    private System.Threading.SendOrPostCallback onGetAgeCompletedDelegate;
    private BeginOperationDelegate onBeginOpenDelegate;
    private EndOperationDelegate onEndOpenDelegate;
    private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
    private BeginOperationDelegate onBeginCloseDelegate;
    private EndOperationDelegate onEndCloseDelegate;
    private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
    public Service1Client() {
    }
    public Service1Client(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }
    public Service1Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }
    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }
    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

我也不知道它是否与问题相关,但在Reference.cs文件中我发现了这个:

 public void GetAgeAsync(string myName) {
        this.GetAgeAsync(myName, null);
    }

应该是空的吗?这个问题有什么解决办法吗?
EDIT: To PoweredByOrange。我的网络。配置是:

 <?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="Database1ConnectionString" connectionString="Data Source=(LocalDB)'v11.0;AttachDbFilename=|DataDirectory|'Database1.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

我的合同是:

    namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
         string GetAge(string myName);
    }
}

我不确定你所说的"服务实现对象"是什么意思(对不起,我是初学者),但我在Reference.cs中发现了这一点(如果我发布了错误的代码,请纠正我)

public partial class GetAgeCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
    private object[] results;
    public GetAgeCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
            base(exception, cancelled, userState) {
        this.results = results;
    }

编辑:额外的参考。cs代码:

public void GetAgeAsync(string myName, object userState) {
        if ((this.onBeginGetAgeDelegate == null)) {
            this.onBeginGetAgeDelegate = new BeginOperationDelegate(this.OnBeginGetAge);
        }
        if ((this.onEndGetAgeDelegate == null)) {
            this.onEndGetAgeDelegate = new EndOperationDelegate(this.OnEndGetAge);
        }
        if ((this.onGetAgeCompletedDelegate == null)) {
            this.onGetAgeCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetAgeCompleted);
        }
        base.InvokeAsync(this.onBeginGetAgeDelegate, new object[] {
                    myName}, this.onEndGetAgeDelegate, this.onGetAgeCompletedDelegate, userState);
    }

客户端代理没有创建方法

由于您正在使用基于事件的异步形式的操作,因此您需要这样的内容:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
    proxy.GetAgeAsyncCompleted += SetAgeText;
    string myName = "Nick";
    proxy.GetAgeAsync(myName);
}
private void SetAgeText(object sender, ServiceReference1.GetAgeCompletedEventArgs e)
{
    AgeTxtBlck.Text = (string) e.Result[0];
}