Integration of information from a Web Service

2

I'm having a bit of trouble creating a Web Service.

I have a MySQL database simple to register people.

I already have my methods for performing CRUD operations in a classe . which were used in an application Windows Forms

I want my CRUD operations to start working from requests for Web service .

So I understand that I can publish Web Service using IIS , so any device that has my address using a rede local can access my Web Service.

To construct this type of functionality I am thinking of migrating my methods and configuration of connections to a project of type ASP.NET Web Service Application

With a project with methods of type [WebMethod] properly configured I will be able to use the methods in any language of programming?

I have vague knowledge about web services, but from what I understood the main purpose would be to integrate information.

My goal with this project is to advertise a Web Service hosted on my local machine to perform basic operations to understand how integration and consumption of the Web Service works in C# and other linguagens de programação .

    
asked by anonymous 17.03.2016 / 15:20

1 answer

2
  

I already have my methods to perform CRUD operations ready in one   class. which were used in a Windows Forms application

You can use yes, just call them within a web services method.

  

So I understand I can publish Web Service using IIS, so   any device that has my address using a local area network can   access my Web Service.

Yes, you can publish to IIS , if your network was accessible by the device it will connect with your web services normally.

  

With a project with methods of type [WebMethod] properly   configured I will be able to use the methods in any language of   programming?

Ideally, it is in what you have more knowledge, but it can be done in other languages.

  

I have vague knowledge about web services, but from what I understand   that the main objective would be the integration of information.

Web service is a solution used in systems integration and communication between different applications

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using WebServiceRohr.Classes.Banco;
using RohrLib.Classes;
using WebServiceRohr.Classes;
using RohrApp.Classes;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace WebService
{
    /// <summary>
    /// 
    /// </summary>
    [WebService(Description = "Web Service to Work with the Mobile Application", Namespace = "www.seusite.com.br")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]

    public class MobileWebService1 : WebServiceBase
    {

        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod()]
        public void MeuMetodo(Int64 id, String Nome)
        {
            using (var DBCtx = new RohrdbContext())
            {
                try
                {
                    // Aqui eu uso entity framework .... 
                    // faça suas alterações de acordo com suas classes...
                    var Tabela = DBCtx.SuaTabela
                        .Where(O => O.id == id)
                        .ToList();


                    // se precisar retornar dados com json
                    JavaScriptSerializer js = new JavaScriptSerializer();

                    string RespJson = js.Serialize(Tabela);
                    writeJsonData(RespJson);
                }
                catch (Exception exc)
                {
                }
            }
        }

        protected void writeJsonData(string s)
        {
            HttpContext context = this.Context;
            HttpResponse response = context.Response;
            context.Response.ContentType = "application/json";
            byte[] b = response.ContentEncoding.GetBytes(s);
            response.AddHeader("Content-Length", b.Length.ToString());
            response.BinaryWrite(b);
            try
            {
                this.Context.Response.Flush();
                this.Context.Response.Close();
            }
            catch (Exception) { }
        }
    }
}

base class for connections

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using RohrLib.Classes;
using System.Web.UI;

namespace WebServiceRohr.Classes
{
    public class WebServiceBase : WebService
    {
        private Int32 m_VSParams = 0;

        StateBag ViewState = new System.Web.UI.StateBag();

        // Create a new VSParam with a unique name
        public ViewstateParam<T> CreateParam<T>()
        {
            m_VSParams++;
            return new ViewstateParam<T>(ViewState, "VSP__" + m_VSParams.ToString());
        }

        // Usado nos diversos DBConn
        protected ConnectionStringSettings ConnString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["DBConn"];
            }
        }

        protected String ConnString_
        {
            get
            {
                return Convert.ToString(ConfigurationManager.ConnectionStrings["DBConn"]);
            }
        }

        private DBConnection m_DBConn = null;
        public DBConnection DBConn
        {
            get
            {
                if (m_DBConn == null)
                    m_DBConn = new DBConnection(ConnString.ConnectionString);
                return m_DBConn;
            }
        }
    }
}
    
21.03.2016 / 15:30