2014-06-13 141 views
0

我有一個錯誤:Cannot implicitly convert type 'void' to 'string'。我遵循每一步來正確地創建web服務(首先編寫服務,編譯並在瀏覽器中查看它,然後使用「添加服務引用」嚮導創建服務引用)。這裏是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); 
    } 

它應該是無效的嗎?有沒有解決這個問題的方法?
編輯:爲PoweredByOrange。我的web.config是:

<?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; 
    } 

編輯:附加Reference.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); 
    } 
+0

參考被從合同,這是你的地方定義的接口生成。更具體地說,'PhoneApp1.ServiceReference1.IService1'有一個叫做'GetAge'的void方法。您需要更改界面上的簽名並重新生成代理。 – PoweredByOrange

+0

@PoweredByOrange,我只有'字符串GetAge(string myName);'在IService1.cs文件中的[OperationContract]下。我從頭開始這個項目,只添加了這個方法。這是我現在擁有的正確簽名嗎? – Peter

+0

奇怪。你如何生成代理? – PoweredByOrange

回答

1

由於您使用操作的基於事件的異步形式,你需要的東西是這樣的:

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]; 
} 
+0

我有一個錯誤:'找不到類型或命名空間名'GetAgeCompletedEventArgs'(你是否缺少使用指令或程序集引用?)' – Peter

+0

我敢打賭,你可以爲自己弄清楚,但我敢打賭它是真的是'ServiceReference1.GetAgeCompletedEventArgs'。 –

+0

它有幫助,但又出現了一個錯誤:'不能將類型'char'轉換爲'string'。我修正它像'AgeTxtBlck.Text = e.Result;'。 – Peter