Reputation: 761
I have an error: Cannot implicitly convert type 'void' to 'string'
. I followed every step to create webservice properly (Wrote service first, compiled it and viewed it in the browser. Then created a service reference using "add service reference" wizard). Here is the WP code:
private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
string myName = "Nick";
AgeTxtBlck.Text = proxy.GetAgeAsync(myName);
}
And the service implementation is:
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();
}
To make my problem more clear here is the client generated proxy:
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) {
}
Also I don't know if its relevant to the question but in the Reference.cs file I found this:
public void GetAgeAsync(string myName) {
this.GetAgeAsync(myName, null);
}
Is it supposed to be void? Is there any solution to this problem?
EDIT: To PoweredByOrange. My Web.config is:
<?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>
My contract is:
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetAge(string myName);
}
}
And I'm not sure what do you mean by "service implementation object" (sorry, I'm a beginner), but I found this in the Reference.cs (correct me if I posted the wrong code)
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;
}
EDIT: Additional Reference.cs code:
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);
}
Upvotes: 0
Views: 509
Reputation: 161773
Since you are using the event-based async form of the operation, you need something like this:
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];
}
Upvotes: 1