Skip to main content

Microsoft Silverlight

Answered Question Consuming a WCF-Service hosted in a Console-Application from Silverlight.RSS Feed

(0)

My.self
My.self

Member

Member

4 points

10 Posts

Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Hi,

im trying since three days to consume a WCF-Service which is hosted in a console application in a Silberlight2 Beta2 application but is still not succeeded…

My Service app.config looks like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HelloServiceBehavior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="HelloServiceBehavior" name="MilkHost.Service1">
        <endpoint address="http://localhost:8080/HelloService/" binding="basicHttpBinding"
            bindingConfiguration="" contract="MilkHost.IService1" />

        <endpoint address="http://localhost:8080/HelloService/MEX/" binding="mexHttpBinding"
              bindingConfiguration="" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

I start the service in the Main function of my console application in the following manner:

   using (ServiceHost serviceHost = new ServiceHost(typeof(Service1)))
                {
                    // Open the ServiceHostBase to create listeners and start listening for messages.
                    serviceHost.Open();


                    // The service can now be accessed.
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("The service is running in the following account: {0}", WindowsIdentity.GetCurrent().Name);
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();

                    // Close the ServiceHostBase to shutdown the service.
                    serviceHost.Close();

                }


Then I started the console application and clicked “Add service Reference” in my Silverlight project…

The wizard created the following configuration file:


<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" maxBufferSize="65536"
                    maxReceivedMessageSize="65536">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/HelloService/" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="MilkClient.ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

 

 


In my Silverlight page I have written the following code:

public Page()
        {
            InitializeComponent();

            ServiceReference1.Service1Client client = new MilkClient.ServiceReference1.Service1Client();
            client.GetDataCompleted += new EventHandler<MilkClient.ServiceReference1.GetDataCompletedEventArgs>(client_GetDataCompleted);
            client.GetDataAsync(123);
        }

        void client_GetDataCompleted(object sender, MilkClient.ServiceReference1.GetDataCompletedEventArgs e)
        {
            string str = e.Result;   
        }

 

But when I call the “client.GetDataAsync(123)” function I get the exteption:

{System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (404) Not Found.
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at MilkClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetData(IAsyncResult result)
   at MilkClient.ServiceReference1.Service1Client.MilkClient.ServiceReference1.IService1.EndGetData(IAsyncResult result)
   at MilkClient.ServiceReference1.Service1Client.OnEndGetData(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)}

 


 

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7202 points

1,342 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Hi,

Did you add the clientaccesspolicy.xml file to your WCF service?

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

My.self
My.self

Member

Member

4 points

10 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Where should i add this file?

I know that i have to copy this file to the root folder of my application on the IIS...

But when i copy it to the execution folder of my console application nothing happens...

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Hi,

Dont Paste this in Your Console application Folder.

Instead of that Put this file to Your Service Project's folder where service is there.

You Can Create your Service in the Same Aspx Project also which is Hosting your Silverlight Project.

Then Policy file is not Required...

Mark as answer if this post answered your question.

Harsh Bardhan

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7202 points

1,342 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

You should place file at root of IIS.

For more details look here.

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

My.self
My.self

Member

Member

4 points

10 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

But i dont want to host my service on IIS!

I want to run it in a console application!!

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Paste That File in Console Application's Root.....

Mark as answer if this post answered your question.

Harsh Bardhan

My.self
My.self

Member

Member

4 points

10 Posts

Re: Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

I have already tryed!

My clientaccesspolicy.xml file looks like:

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

 

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7202 points

1,342 Posts

Re: Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Did you look this post?

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Can You Specify What Error are you Getting..

 

Mark as answer if this post answered your question.

Harsh Bardhan

My.self
My.self

Member

Member

4 points

10 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

{System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (404) Not Found.
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at MilkClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetData(IAsyncResult result)
   at MilkClient.ServiceReference1.Service1Client.MilkClient.ServiceReference1.IService1.EndGetData(IAsyncResult result)
   at MilkClient.ServiceReference1.Service1Client.OnEndGetData(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)}

 

 

InnerException=null

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

hi,

1)Is Your Console Application and Your silverlight Project is in Same Solution???

2)when you are running Your Silverlight Project your Console Application is Running??

3)You are able to Browse  to Url which you are reffering like: http://localhost:8080/HelloService/ when your Silverlight application is Running...

I think there might Be Problem due to that...

 

Mark as answer if this post answered your question.

Harsh Bardhan

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Consuming a WCF-Service hosted in a Console-Application from Silverlight.

Hello, since you're hosting the WCF service in a console application, you don't have a web server, and thus does not have the concept of "root folder of the web server". You have to find a way to manually serve the cross domain policy file. A common solution is to create a REST service with UriTemplate = "clientaccesspolicy.xml" that returns the content of the policy file. Please refer to this thread.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities