在Azure中运行两个服务

时间:2014-05-07 20:13:12

标签: windows-phone-7 azure windows-phone-8 azure-storage azure-web-roles

我创建了一个WCF文件,我正在运行REST服务,输出XML,这个数据连接到我的WP8,用户可以输入数据,然后在列表视图中调用数据。

我在一个单独的项目中有一个推送通知服务,我想添加,与移动电话连接,但也在与REST服务相同的服务器上运行。

目标:将推送通知集成到应用程序中。

我一直在环顾四周,如果有人可以清除它,就像在我的REST WCF中我添加了一个新项目,其中是一个WCF服务,但是当我尝试添加服务引用它不会出现而且我的云.... service1和2出现错误。然后我尝试将代码放在REST WCF中,这也没有用。

有人能建议最好的方法吗?

namespace WCFServiceWebRole1
{

[ServiceContract]

public interface IService1
{

    [OperationContract]

    void Subscribe(string deviceid, string uri);

    [OperationContract]

    void SendToast(string title, string message);

    [OperationContract]

    void SendTile(string title, string count);

}

}

 namespace WCFServiceWebRole1
{

public class Service1 : IService1
{

    private static Uri uri;

    private static String device_ID;



    // Save the subscription channel with the phone's deviceID to persistant storage

    public void Subscribe(string deviceid, string subscriberUri)
    {

        uri = new Uri(subscriberUri);

        device_ID = deviceid;


    }


    // Send toast notification method

    public void SendToast(string title, string message)
    {

        // build our xml payload as a string

        string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

            "<wp:Notification xmlns:wp=\"WPNotification\">" +

                "<wp:Toast>" +

                    "<wp:Text1>" + title + "</wp:Text1>" +

                "<wp:Text2>" + message + "</wp:Text2>" +

                "</wp:Toast>" +

            "</wp:Notification>";

        // set notification type to 'toast'

        String messageType = "toast";



        SendMessage(uri, toastMessage, messageType, "2");

    }

    // Send tile notification method

    public void SendTile(string title, string count)
    {

        string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

                            "<wp:Notification xmlns:wp=\"WPNotification\">" +

                                "<wp:Tile>" +

                                    "<wp:Count>" + count + "</wp:Count>" +

                                    "<wp:Title>" + title + "</wp:Title>" +

                                "</wp:Tile> " +

                                "</wp:Notification>";

        // set notification type to 'token' for tile

        String messageType = "token";



        SendMessage(uri, tileMessage, messageType, "1");

    }

    private static void SendMessage(Uri uri, String message, String messageType, String 
    notificationClass)
    {

        HttpWebRequest sendNotificationRequest =               
       (HttpWebRequest)WebRequest.Create(uri);

        sendNotificationRequest.Method = "POST";

        sendNotificationRequest.Headers = new WebHeaderCollection();

        sendNotificationRequest.ContentType = "text/xml";

        sendNotificationRequest.Headers.Add("X-NotificationClass", notificationClass);

        sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", messageType);

        byte[] strBytes = new UTF8Encoding().GetBytes(message);

        sendNotificationRequest.ContentLength = strBytes.Length;

        using (Stream requestStream = sendNotificationRequest.GetRequestStream())
        {

            requestStream.Write(strBytes, 0, strBytes.Length);

        }



        try
        {

            var response = (HttpWebResponse)sendNotificationRequest.GetResponse();

            var notiticationStatus = response.Headers["X-NotificationStatus"];

            var notitificationChannelStatus = response.Headers["X-SubscriptionStatus"];

            var deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

        }

        catch (Exception ex)
        {

            Console.Write(ex.ToString());

            // Ignoring the response.

            // In a production application you should review the response and

            // code appropriate for the specific response.

            // http://msdn.microsoft.com/en-us/library/ff941100%28v=VS.92%29.aspx

        }

    }




}

REST

  namespace WCFServiceWebRole1
{



public class Service1 : IService1
{
    public bool addProfile(string guid, string firstname, string surname)
    {

        // code to add a new user profile to SQL database



        // parse guid string to proper guid type

        Guid guid_new = Guid.Parse(guid);

        // creates a new instance of our database objects (user table in this case)

        using (var context = new sad_Rest_serviceEntities())
        {

            // add a new object (or row) to our user profile table

            context.user_profile.Add(new user_profile()

           // bind each database column to the parameters we pass in our method

           // guid, firstname, surname, and email

            {

                guid = guid_new,

                firstname = firstname,

                surname = surname

            });

            // commit changes to the user profile table

            context.SaveChanges();

            return true;

        }

    }

    public Users[] viewProfilesXML(string guid)
    {

        // get the connections string stored in the web.config file as a string

        string connectionString =    
 ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        // create  new sql connections using the connection string

        SqlConnection thisConnection = new SqlConnection(connectionString);

        // create a new sql command called getUsers

        SqlCommand getUsers = thisConnection.CreateCommand();

        // create a temp list to store the rows of users returned from the database

        List<Users> users = new List<Users>();

        // open the sql connection and construct the select query

        thisConnection.Open();

        string sql = "select * from user_profile where guid=@guid";

        // paramertise your query to stop sql injections!

        getUsers.Parameters.AddWithValue("@guid", guid);

        getUsers.CommandText = sql;

        // create an sql data adapter using the getUsers query

        SqlDataAdapter da = new SqlDataAdapter(getUsers);



        DataSet ds = new DataSet();

        da.Fill(ds, "user_profile");

        // for every row returned call our DataContract in IService1.cs

        foreach (DataRow dr in ds.Tables["user_profile"].Rows)
        {

            users.Add(new Users()

            {

                Id = Convert.ToInt32(dr[0]),

                Guid = Convert.ToString(dr[1]),

                Firstname = Convert.ToString(dr[2]),

                Surname = Convert.ToString(dr[3])

            });

        }

        //Return data for XML output

        thisConnection.Close();

        return users.ToArray();

    }       
}


 namespace WCFServiceWebRole1
 {


[ServiceContract]

public interface IService1
{




    [OperationContract]

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,

    UriTemplate = "users?guid={guid}&firstname={firstname}&surname={surname}")]

    bool addProfile(string guid, string firstname, string surname);




    [OperationContract]

    [WebInvoke(Method = "GET",

     ResponseFormat = WebMessageFormat.Xml,

     BodyStyle = WebMessageBodyStyle.Bare,

    UriTemplate = "viewusers?format=xml&guid={guid}")]

    Users[] viewProfilesXML(string guid);
}

    [DataContract]
    public class Users
    {

        [DataMember]

        private int id;

        public int Id
        {

            get { return id; }

            set { id = value; }

        }

        [DataMember]

        private string guid;

        public string Guid
        {

            get { return guid; }

            set { guid = value; }

        }

        [DataMember]

        private string firstname;

        public string Firstname
        {

            get { return firstname; }

            set { firstname = value; }

        }

        [DataMember]

        private string surname;

        public string Surname
        {

            get { return surname; }

            set { surname = value; }

        }


}

0 个答案:

没有答案