What I'm trying to do is demonstrate a silverlight application that talks to a wcf webservice that then talks to a database. When I run locally the silverlight client has a service reference to http://localhost:49797/DemoService.svc. DemoService has two methods that submit and get data from a database. When run locally it works fine and indeed submits data to my discountasp.net located database. Once it's published however, it can't use localhost. So now I try to reconfigure my service reference tohttp://uiprototype.web702.discountasp.net/DemoService.svcbut when I try this I get the error message: "This collection already contains and address with the scheme http. There can be at most one address per scheme in this collection." (By the way I don't use the WCF Service template. I use the Silverlight Enabled WCF Service Template. The code generated is what you see below. There is no APP_CODE directory.) [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class DemoService { // Return the list of valid links [OperationContract] public List<DemoTable> GetRows() { DataClasses1DataContext db = new DataClasses1DataContext(); var selected_rows = from rows in db.DemoTables select rows; return selected_rows.ToList(); } // Insert a new link into the database [OperationContract] public void InsertData(string testItem1, string testItem2) { DataClasses1DataContext db = new DataClasses1DataContext(); // Create a new Order object. DemoTable row = new DemoTable { Key = Guid.NewGuid(), TestItem1 = testItem1, TestItem2 = testItem2 }; // Add the new object to the Orders collection. db.DemoTables.InsertOnSubmit(row); // Submit the change to the database. try { db.SubmitChanges(); } catch (Exception e) { Console.WriteLine(e); // Make some adjustments. // ... // Try again. db.SubmitChanges(); } }
Ok, I figured it out. I had to make the following changes: <%@ ServiceHost Language="C#" Debug="true" Service="WcfSqlDemoWeb.DemoService" Factory="WcfSqlDemoWeb.MyServiceHostFactory" CodeBehind="DemoService.svc.cs" %> public class MyServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { // Specify the exact URL of your web service Uri webServiceAddress = new Uri("http://uiprototype.web702.discountasp.net/DemoService.svc"); ServiceHost webServiceHost = new ServiceHost(serviceType, webServiceAddress); return webServiceHost; } }
I incorporated all the things I learned into a step-by-step tutorial on how to build a datacentric silverlight web app that uses WCF andLINQ to submit and retreive data from a database. You can check it out at my blog at http://http://www.silverlightwebapps.com/Tutorials.aspx.
I had the same exact problem so I added <%@ ServiceHost Language="C#" Factory="SC2.Web.MyServiceHostFactory" Debug="true" Service="SC2.Web.Service1" CodeBehind="Service1.svc.cs" %> and using System; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.Collections.Generic; using System.Text; namespace SC2.Web { [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 { [OperationContract] public void DoWork() { // Add your operation implementation here return; } [OperationContract] public string GetName() { // Add your operation implementation here return "John"; } // Add more operations here and mark them with [OperationContract] } public class MyServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { // Specify the exact URL of your web service Uri webServiceAddress = new Uri("http://simplecoder.net/Service1.svc"); ServiceHost webServiceHost = new ServiceHost(serviceType, webServiceAddress); return webServiceHost; } } } but now I get this --> HELP Webpage Script Errors User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; InfoPath.2; MS-RTC LM 8; .NET CLR 3.5.30729; .NET CLR 3.0.30618) Timestamp: Thu, 6 Nov 2008 17:45:05 UTC Message: Unhandled Error in Silverlight 2 Application An exception occurred during the operation, making the result invalid. Check InnerException for exception details. at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at SC2.ServiceReference1.GetNameCompletedEventArgs.get_Result() at SC2.Page.client_GetNameCompleted(Object sender, GetNameCompletedEventArgs e) at SC2.ServiceReference1.Service1Client.OnGetNameCompleted(Object state) Line: 1 Char: 1 Code: 0 URI: http://www.simplecoder.net/SC2TestPage.aspx