zoltan vatadi

zoltan vatadi

  • NA
  • 1
  • 3.9k

Cannot add Service as ServiceReference in Silverlight Project

Sep 9 2010 5:03 PM
Hi!

I tried to create a Silverlight-enabled WCF Service, but when i try to add it as a service reference to my project i always face an error.
Here's .cs file:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;

namespace TODO.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    public class TODO_db_Service
    {
        public List<Users> SelectUsersWhere(string where)
        {

            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
            MySql.Data.MySqlClient.MySqlDataReader myData;
            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            List<Users> userlist = new List<Users>();

            string commandQuery = "Select userName, password, emailAddress FROM Users WHERE " + where + ";";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = commandQuery;

                myData = cmd.ExecuteReader();

                myData.Read();

                if (myData.HasRows)
                {
                    do
                    {
                        string userName = myData.GetString("userName").ToString();
                        string password = myData.GetString("password").ToString();
                        string emailAddress = myData.GetString("emailAddress").ToString();

                        Users instance = new Users();
                        instance.usernameField = userName;
                        instance.passwordField = password;
                        instance.emailField = emailAddress;

                        userlist.Add(instance);
                    } while (myData.Read());

                    myData.Close();
                    conn.Close();
                }
                myData.Close();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }
            return userlist;
        }

        public void InsertIntoUsers(string userName, string password, string emailAddress)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            string insertQuery = "INSERT INTO Users (userName, password, emailAddress) VALUES ('"
                + userName + "', '" + password + "', '" + emailAddress + "');";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = insertQuery;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }

        }

        public void InsertIntoProjects(string projectTitle, string description, string expirationDate, string assosiatedEmailAddress)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            string insertQuery;

            if (expirationDate == "null")
            {
                insertQuery = "INSERT INTO Projects (projectTitle, description, expirationDate, assosiatedEmailAddress) VALUES ('"
                   + projectTitle + "', '" + description + "', " + expirationDate + ", '" + assosiatedEmailAddress + "');";
            }
            else
            {
                insertQuery = "INSERT INTO Projects (projectTitle, description, expirationDate, assosiatedEmailAddress) VALUES ('"
                  + projectTitle + "', '" + description + "', '" + expirationDate + "', '" + assosiatedEmailAddress + "');";
            }
            conn.ConnectionString = connectionString;

            Console.WriteLine(insertQuery);

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = insertQuery;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }
        }

        public void InsertIntoTasks(string taskName, string description, string expirationDate,
                            string assosiatedEmailAddress, int assosiatedTaskId)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            string insertQuery;
            if (expirationDate == "null")
            {
                insertQuery = "INSERT INTO Tasks (taskName, description, expirationDate, assosiatedEmailAddress, assosiatedProjectId) VALUES ('"
                 + taskName + "', '" + description + "', " + expirationDate + ", '"
                 + assosiatedEmailAddress + "', '" + assosiatedTaskId + "');";
            }
            else
            {
                insertQuery = "INSERT INTO Tasks (taskName, description, expirationDate, assosiatedEmailAddress, assosiatedProjectId) VALUES ('"
                 + taskName + "', '" + description + "', '" + expirationDate + "', '"
                 + assosiatedEmailAddress + "', '" + assosiatedTaskId + "');";
            }




            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = insertQuery;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }
        }

        public string RegisterUser(string userName, string password, string emailAddress)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
            MySql.Data.MySqlClient.MySqlDataReader myData;

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            List<Users> userlist = new List<Users>();
            string where = "WHERE emailAddress LIKE '" + emailAddress + "'";
            string commandQuery = "Select userName, password, emailAddress FROM Users " + where + ";";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = commandQuery;

                myData = cmd.ExecuteReader();
                myData.Read();

                if (myData.HasRows)
                {
                    myData.Close();
                    conn.Close();

                    return "E-mail Address taken, choose a new one!";
                }
                else
                {
                    myData.Close();
                    conn.Close();
                    InsertIntoUsers(userName, password, emailAddress);

                    return "Registration completed!";
                }


            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }

            conn.Close();
            return "error";
        }

        public Users LoginUser(string emailAddress, string password)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
            MySql.Data.MySqlClient.MySqlDataReader myData;
            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            string where = "WHERE emailAddress LIKE '" + emailAddress + "' AND password LIKE '" + password + "'";

            string commandQuery = "Select userName, password, emailAddress FROM Users " + where + ";";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = commandQuery;

                myData = cmd.ExecuteReader();

                myData.Read();

                if (myData.HasRows)
                {
                    string db_userName = myData.GetString("userName").ToString();
                    string db_password = myData.GetString("password").ToString();
                    string db_emailAddress = myData.GetString("emailAddress").ToString();

                    Users instance = new Users();
                    instance.usernameField = db_userName;
                    instance.passwordField = db_password;
                    instance.emailField = db_emailAddress;

                    myData.Close();
                    conn.Close();
                    return instance;
                }

            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }

            conn.Close();
            return null;
        }

        public List<Tasks> SelectTasksWhere(string where)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
            MySql.Data.MySqlClient.MySqlDataReader myData;
            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            List<Tasks> list = new List<Tasks>();
            string commandQuery = "Select id, taskName, description, expirationDate, assosiatedEmailAddress, assosiatedProjectId FROM Tasks WHERE "
                                    + where + ";";
            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = commandQuery;

                myData = cmd.ExecuteReader();

                myData.Read();

                if (myData.HasRows)
                {
                    do
                    {

                        int id = myData.GetInt32("id");
                        string taskName = myData.GetString("taskName").ToString();
                        string description = myData.GetString("description").ToString();
                        DateTime expirationDate;
                        if (!myData.IsDBNull(3)) { expirationDate = Convert.ToDateTime(myData.GetString("expirationDate").ToString()); }
                        else { expirationDate = DateTime.MaxValue; }

                        string assosiatedEmailAddress = myData.GetString("assosiatedEmailAddress").ToString();
                        int assosiatedProjectId = myData.GetInt32("assosiatedProjectId");

                        Tasks instance = new Tasks();
                        instance._id = id;
                        instance._taskName = taskName;
                        instance._description = description;
                        instance._expirationDate = expirationDate;
                        instance._assosiatedEmailAddress = assosiatedEmailAddress;
                        instance._assosiatedProjectId = assosiatedProjectId;

                        list.Add(instance);

                    } while (myData.Read());
                }

                myData.Close();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }

            return list;
        }

        public List<Projects> SelectProjectsWhere(string where)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
            MySql.Data.MySqlClient.MySqlDataReader myData;
            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            List<Projects> list = new List<Projects>();
            string commandQuery = "Select id, projectTitle, description, expirationDate, assosiatedEmailAddress FROM Projects WHERE "
                                    + where + ";";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = commandQuery;

                myData = cmd.ExecuteReader();

                myData.Read();

                if (myData.HasRows)
                {
                    do
                    {

                        int id = myData.GetInt32("id");
                        string projectTitle = myData.GetString("projectTitle").ToString();
                        string description = myData.GetString("description").ToString();
                        DateTime expirationDate;
                        if (!myData.IsDBNull(3)) { expirationDate = Convert.ToDateTime(myData.GetString("expirationDate").ToString()); }
                        else { expirationDate = DateTime.MaxValue; }

                        string assosiatedEmailAddress = myData.GetString("assosiatedEmailAddress").ToString();

                        Projects instance = new Projects();
                        instance._id = id;
                        instance._projectTitle = projectTitle;
                        instance._description = description;
                        instance._expirationDate = expirationDate;
                        instance._assosiatedEmailAddress = assosiatedEmailAddress;

                        list.Add(instance);

                    } while (myData.Read());
                }

                myData.Close();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }

            return list;

        }

        public void DeleteProjectAndItsTasksWhereId(int Id)
        {
            string deleteQuery = "DELETE FROM Projects WHERE id = " + Id + ";";
            string deleteTaskQuery = "DELETE FROM TASKS WHERE assosiatedProjectId = " + Id + ";";

            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = deleteQuery;
                cmd.ExecuteNonQuery();
                cmd.CommandText = deleteTaskQuery;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }
        }

        public void DeleteProjectUpdateItsTasksWhereId(int Id)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";
            string deleteQuery = "DELETE FROM Projects WHERE id = " + Id + ";";
            string updateQuery = "UPDATE Tasks SET assosiatedProjectId = 1 WHERE assosiatedProjectId = " + Id + ";";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = deleteQuery;
                cmd.ExecuteNonQuery();
                cmd.CommandText = updateQuery;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }

        }

        public void DeleteTaskWhere(int Id)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string connectionString = "server=localhost;User Id=root;Password=root;Persist Security Info=True;database=todo_db";

            string deleteQuery = "DELETE FROM Tasks WHERE id = " + Id + ";";

            conn.ConnectionString = connectionString;

            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = deleteQuery;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            }
        }

    }

    public class Users
    {
        public string usernameField { get; set; }

        public string passwordField { get; set; }

        public string emailField { get; set; }
    }

    public class Projects
    {
        public int _id { get; set; }
        public string _projectTitle { get; set; }
        public string _description { get; set; }
        public string _comment { get; set; }
        public DateTime _expirationDate { get; set; }
        public List<Tasks> _tasks = new List<Tasks>();
        public string _assosiatedEmailAddress { get; set; }
    }

    public class Tasks
    {
        public int _id { get; set; }
        public string _taskName { get; set; }
        public string _description { get; set; }
        public string _comment { get; set; }
        public DateTime _expirationDate { get; set; }
        public string _assosiatedEmailAddress { get; set; }
        public int _assosiatedProjectId { get; set; }
    }

}

I've set everythig public, changed the getters and setters, removed the constructors but still i get the error that:
There was an error downloading the metadata from the address. Please verify that you entered a valid address.

the details say:
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: The type 'TODO.Web.TODO_db_Service', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.]
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +51902
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +1440
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +44
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +615

[ServiceActivationException: The service '/TODO_db_Service.svc' cannot be activated due to an exception during compilation.  The exception message is: The type 'TODO.Web.TODO_db_Service', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +679246
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, String routeServiceVirtualPath, Boolean flowContext, Boolean ensureWFService) +234
   System.ServiceModel.Activation.HttpHandler.ProcessRequest(HttpContext context) +24
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
What is the problem?

Answers (1)