Aklımda Kalası Kelimeler

* давайте работать вместе
* Zarf ve Mazruf, Zerafet(xHoyratlık) ile aynı kökten(za-ra-fe) gelir
* Bedesten
* Suç subuta ermiştir - Suç sabit olmuştur
Global Assembly Cache etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Global Assembly Cache etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

4 Ağustos 2011 Perşembe

Web User Control 'den Web Servisi çağırmak

Bir önceki makalemde bir web servisinden dll ler oluşturma ve bunu GAC'a yüklemeden bahsetmiştim.

Şimdi GAC'daki bu DLL'i kullanarak bir web user control içinden, code behind olmadan .ascx dosyasından nasıl bu servise erişirizi yazayım.

WUC'da .ascx dosyasında tüketmek istiyorum çünkü bu dosyayı daha sonra derlemek istemiyorum.


Kısa ve özet haliyle:
<%@ Control Language="C#" ClassName="wuc" %>
<%@ Import Namespace="System.Reflection" %>


<script runat="server">
protected override void CreateChildControls()
{
base.CreateChildControls();
// deneme.dll 'imiz GAC'a yüklü ve çekiyoruz
object obj = Activator.CreateInstance("deneme, Version=0.0.0.0, Culture=neutral, PublicKeyToken=dc5cbb4d7c01cc5e", "Service1").Unwrap();
Type tip = obj.GetType();
MethodInfo miTopla = tip.GetMethod("HelloWorld");

btn.Text = miTopla.Invoke(obj, null).ToString();
}
</script>

<asp:Button runat="server" ID="btn" Text="asdasd" />


işte daha detaylandırdığım ascx in codu:

<%@ Control Language="C#" ClassName="WebUserControl" %>
<%@ Import Namespace="System.Reflection" %>
<script runat="server">

public class GenerateObject
{
public enum WebServicesClass
{
HastaRaporlariServisi, Yardimci
}
const string sAsmNameHastaRaporlari = "HastaRaporlari, Version=0.0.0.0, Culture=neutral, PublicKeyToken=0b0b7e799bda4cb2";
const string sHastaRaporlariServisi = "HastaRaporlariServisi";
const string sAsmNameYardimci = "HastaRaporlari, Version=0.0.0.0, Culture=neutral, PublicKeyToken=0b0b7e799bda4cb2";
const string sYardimciServisi = "YardimciServisi";

public static object CreateInstance(WebServicesClass _wsc)
{
try
{
switch (_wsc)
{
case WebServicesClass.HastaRaporlariServisi:
return Activator.CreateInstance(sAsmNameHastaRaporlari, sHastaRaporlariServisi).Unwrap();
case WebServicesClass.Yardimci:
return Activator.CreateInstance(sAsmNameYardimci, sYardimciServisi).Unwrap();
default:
return null;
}
}
catch (Exception ex)
{
throw;
}
}

public static MethodInfo GetMethod(object _obj, string _sMethodName)
{
try
{
Type tip = _obj.GetType();
MethodInfo mi = tip.GetMethod(_sMethodName);
return mi;
}
catch (Exception ex)
{
throw;
}
}

public static object Invoke(object _obj, string _sMethodName, params object[] _params)
{
object result = GenerateObject.GetMethod(_obj, _sMethodName).Invoke(_obj, _params);
return result;
}


public static object Invoke(WebServicesClass _wsc, string _sMethodName, params object[] _params)
{
object instance = GenerateObject.CreateInstance(_wsc);
object result = GenerateObject.GetMethod(instance, _sMethodName).Invoke(instance, _params);
return result;
}

}
protected override void CreateChildControls()
{
base.CreateChildControls();

btn.Text = GenerateObject.Invoke(GenerateObject.WebServicesClass.HastaRaporlariServisi,"tistring",null).ToString();
}
</script>
<asp:Button runat="server" ID="btn" Text="asdasd" />

Yukarıdaki uzun kodu değişmeyecek olan GenerateObject sınıfını bir .inc dosyasına koyarak ascx'e çekmek isterseniz şu şekilde olacaktır:
<script runat="server">

public class GenerateObject
{
public enum WebServicesClass
{
HastaRaporlariServisi, Yardimci
}
const string sAsmNameHastaRaporlari = "HastaRaporlari, Version=0.0.0.0, Culture=neutral, PublicKeyToken=0b0b7e799bda4cb2";
const string sHastaRaporlariServisi = "HastaRaporlariServisi";
const string sAsmNameYardimci = "HastaRaporlari, Version=0.0.0.0, Culture=neutral, PublicKeyToken=0b0b7e799bda4cb2";
const string sYardimciServisi = "YardimciServisi";

public static object CreateInstance(WebServicesClass _wsc)
{
try
{
switch (_wsc)
{
case WebServicesClass.HastaRaporlariServisi:
return Activator.CreateInstance(sAsmNameHastaRaporlari, sHastaRaporlariServisi).Unwrap();
case WebServicesClass.Yardimci:
return Activator.CreateInstance(sAsmNameYardimci, sYardimciServisi).Unwrap();
default:
return null;
}
}
catch (Exception ex)
{
throw;
}
}

public static MethodInfo GetMethod(object _obj, string _sMethodName)
{
try
{
Type tip = _obj.GetType();
MethodInfo mi = tip.GetMethod(_sMethodName);
return mi;
}
catch (Exception ex)
{
throw;
}
}

public static object Invoke(object _obj, string _sMethodName, params object[] _params)
{
object result = GetMethod(_obj, _sMethodName).Invoke(_obj, _params);
return result;
}


public static object Invoke(WebServicesClass _wsc, string _sMethodName, params object[] _params)
{
object instance = CreateInstance(_wsc);
object result = GetMethod(instance, _sMethodName).Invoke(instance, _params);
return result;
}

}
</script>

Bu durumda ascx dosyamızda:
<%@ Control Language="C#" ClassName="WebUserControl" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="FMC.Google.Chart.Visualization" %>

<!-- #include virtual="Generator.inc" -->

<script runat="server">
protected override void CreateChildControls()
{
base.CreateChildControls();

btn.Text = GenerateObject.Invoke(GenerateObject.WebServicesClass.HastaRaporlariServisi, "tistring", null).ToString();
}
</script>


GenerateObject sınıfı ile daha hızlı bir şekilde metod çağrısı yapabiliyorum.

Bu ascx dosyasını kullanacağım Default.aspx dosyam:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<uc1:WebUserControl ID="WebUserControl1" runat="server" />

</div>
</form>
</body>
</html>

2 Ağustos 2011 Salı

Bir web servisinin çekilerek cs'inin oluşturulması ve ardından dll'e çevirilmesi.

Output tabına pek bakmayız ama ben bir bakayım dedim ve şunları gördüm:
------ Build started: Project: C:\...\WebSite4\, Configuration: Debug Any CPU ------
Validating Web Site
Building directory '/WebSite4/'.

C:\Users\Administrator\Documents\Visual Studio 2008\WebSites\WebSite4\WebUserControl2.ascx.cs(7,7): error CS0246: The type or namespace name 'FMCTR' could not be found (are you missing a using directive or an assembly reference?)
Validation Complete
------ Build started: Project: CinsiyetOranlari, Configuration: Debug Any CPU ------
C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"..\..\GoogleChartAPI\trunk\bin\Debug\FMC.Google.Chart.dll" /reference:"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.EnterpriseServices.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.Services.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /keyfile:Properties\CinsiyetOranlari.snk /optimize- /out:obj\Debug\CinsiyetOranlari.dll /target:library "Properties\AssemblyInfo.cs" CinsiyetOranlari\CinsiyetOranlari.cs Properties\Settings.Designer.cs "Web References\CinsiyetDagilimi\Reference.cs"
D:\Projeler\FMC.FMedula.Rapor.WebPart\CinsiyetOranlari\CinsiyetOranlari\CinsiyetOranlari.cs(97,30): warning CS0168: The variable 'ex' is declared but never used

Compile complete -- 0 errors, 1 warnings
CinsiyetOranlari -> D:\Projeler\FMC.FMedula.Rapor.WebPart\CinsiyetOranlari\bin\Debug\CinsiyetOranlari.dll
========== Build: 1 succeeded or up-to-date, 1 failed, 0 skipped ==========


error CS1668 : Warning as error : Invalid search path 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib' specified in 'LIB 
environment variable' -- 'The system cannot find the path specified. '
Hatası oluştuğunda C:\Program Files\Microsoft SDKs\Windows\v6.0A klasörüne boş bir lib dosyası oluşturmak yeterli.
ref:frazzleddad.blogspot.com

Bir sınıfı DLL'e çevirmek


Özetle Kodu:
csc /t:library /out:MyCodeLibrary.dll /recurse:*.cs /doc:myDoc.xml /keyfile:C:\MyKeyPair\myKeypair.snk

Kaynaklar:
MSDN
www.akadia.com
misc.feedfury.com
http://www.lcbridge.nl/vision/publickeytoken.htm

Bu DLL'i GAC'a yüklemek için


Ref: Gacutil.exe (Global Assembly Cache Tool)
c:\>gacutil.exe -if c:\temp\servis.dll



cls
echo Web Servisinden bir SINIF olusturuluyor:
@SET WSDL="c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\wsdl.exe"

%WSDL% /language:cs /out:c:\temp\servis.cs http://localhost:85/raporws/hastaraporlari.asmx?WSDL



echo Bu SINIFI strongly name type haline getirmek icin .snk dosyasi olusturuluyor:
@SET SN="c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\sn.exe"

%SN% -k c:\temp\servis.snk




echo Bu SNK dosyasiyla SINIF hem DLL, hemde PublicKeyToken sahibi oluyor:
@SET CSC="c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"

%CSC% /target:library /out:c:\temp\servis.dll c:\temp\servis.cs /keyfile:C:\temp\servis.snk



echo DLL imizi GAC a yukluyoruz:
@SET GACUTIL="c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe"

%GACUTIL% -if c:\temp\servis.dll


Birazda bu kaynaklara bakılabilir:
Essential Batch File Commands

GAC içindeki assembly hakkında bilgi almak


C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64>gacutil.exe /l deneme.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version 3.5.30729.1
Copyright (c) Microsoft Corporation. All rights reserved.

The Global Assembly Cache contains the following assemblies:
deneme, Version=0.0.0.0, Culture=neutral, PublicKeyToken=2894795580ed377b,
cessorArchitecture=MSIL

Number of items = 1

ASMX Servisleri otomatik olarak DLL'e çeviren ve STRONGLY NAMED TYPE'a çeviren ve GAC'a yükleyen


cls
@echo off

@SET folder=D:\Projeler\FMCTR.WebServices\FMCTR.FMedula.Rapor.WS\dllOlusturGonder\
@SET serviceFolder=D:\Projeler\FMCTR.WebServices\FMCTR.FMedula.Rapor.WS\
@SET outputDLL=toplu.dll
@SET hostName=http://localhost:1866/

@SET asmxAdresi=HastaRaporlari
@SET dosyaAdi=%asmxAdresi%

@SET asmxFiles=(HastaRaporlari Yardimci)

rem for %%a in %asmxFiles% do (
for %%a in (%serviceFolder%*.asmx) do (
echo off
call :MakeCS %%a
)
goto end


:MakeCS
@SET WSDL="C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\wsdl.exe"
%WSDL% /language:cs /out:%folder%%1.cs %hostName%%1.asmx?WSDL 

call :CreateSNK %1
goto end


:CreateSNK
@SET SN="C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sn.exe"
%SN% -k %folder%%1.snk

call :MakeDLL %1
goto end

:MakeDLL
@echo %1  
@SET CSC="C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe"
%CSC% /target:library /out:%folder%%1.dll %folder%%1.cs /keyfile:%folder%%1.snk

call :LoadGAC %1
goto end


:LoadGAC
echo on
@SET GACUTIL="C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\gacutil.exe"
%GACUTIL% -if %folder%%1.dll
goto end


:MergeDll
@echo %1  
@SET ILMerge="C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe"
%ILMerge% %1 /out=%folder%%outputDLL%
goto end

:end


ILMerge ile assembly dosyalarını birleştirmek


Kaynaklar:
www.developerfusion.com
www.codeproject.com

We made a command line tool which takes parameters and sends e-mails. We wanted to distribute this as an .exe but couldn't because it referenced a .dll. This meant that we had to send along the .dll and that it had to be in the same directory as the .exe. We wanted a way to have just one .exe but Visual Studio .NET doesn't offer this (a linker). Luckily, at the Microsoft Research Download Page there is a free tool called ILMerge which does just this. This is how you use it:
1.download ILMerge
2. put "ILMerge.exe" in your \WINNT directory
3.In VS.NET, right click project, Properties, Common Properties, Build Events
4. In "Post-build Event Command Line" enter:
ilmerge /out:$(TargetDir)YOURAPPNAME.exe $(TargetPath) $(TargetDir)YOURDLLNAME.dll
5.Then compile the Release version (not the debug version).
6.In your "bin\Release" directory, you will find a YOURAPPNAME.exe which can be run on its own without the .dll.


9 Mart 2011 Çarşamba

GAC dan dll leri çeken uygulama

Ben yazmadım. Bu abi şurada yazmış.

Süper işime yaradı Allah ondan razı olsun :)

using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
string[] sourceFiles = new string[]
{
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.CrystalReports.Engine\10.5.3700.0__692fbea5521e1304\CrystalDecisions.CrystalReports.Engine.dll",
@"C:\Windows\assembly\GAC\CrystalDecisions.Enterprise.Framework\10.5.3700.0__692fbea5521e1304\CrystalDecisions.Enterprise.Framework.dll",
@"C:\Windows\assembly\GAC\CrystalDecisions.Enterprise.InfoStore\10.5.3700.0__692fbea5521e1304\CrystalDecisions.Enterprise.InfoStore.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.KeyCode\10.5.3700.0__692fbea5521e1304\CrystalDecisions.KeyCode.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.ClientDoc\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.ClientDoc.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.CommLayer\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.CommLayer.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.Controllers\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.Controllers.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.DataDefModel\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.DataDefModel.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.DataSetConversion\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.DataSetConversion.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.ReportDefModel\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.ReportDefModel.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportAppServer.XmlSerialize\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportAppServer.XmlSerialize.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.ReportSource\10.5.3700.0__692fbea5521e1304\CrystalDecisions.ReportSource.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.Shared\10.5.3700.0__692fbea5521e1304\CrystalDecisions.Shared.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.Web\10.5.3700.0__692fbea5521e1304\CrystalDecisions.Web.dll",
@"C:\Windows\assembly\GAC_MSIL\CrystalDecisions.Windows.Forms\10.5.3700.0__692fbea5521e1304\CrystalDecisions.Windows.Forms.dll",
};

foreach (string file in sourceFiles)
File.Copy(file, Path.Combine(@"C:\Temp\CrystalReportsAssemblies", Path.GetFileName(file)));
}
}