레이블이 C#인 게시물을 표시합니다. 모든 게시물 표시
레이블이 C#인 게시물을 표시합니다. 모든 게시물 표시

2023/02/28

Visual Studio CS0518 에러 메시지 표시 관련

Visual Studio 2019 에서, 이전 버전에서 작업했던 ASP.NET MVC 프로젝트를 로드하면, cshtml 에서 c# 언어쪽 문자열에서 CS0518 개체 타입 관련 경고(오류) 표시가 쭈욱 나는 증상..

빈 새프로젝트를 생성해서 비교해 보니, 이건 또 경고창이 안뜬다. web.config 에서 compilers 항목의 버전 지정값이 다르긴 한데..

그래서, 다시 기존 프로젝트를 열었던 VS 를 종료후 다시 실행하니깐.. 응??? 이건 왜 또 경고창이 발생안해 ???.....;;  cshtml 파일 razor 타입 코딩된 페이지 열면 경고창이 떳는데, 메쏘드 부분만 잘라내기 후 다시 그자리에 붙이기 하니, 경고창이 없어진다.. ㅎㅎ 

이건 아무래도.. Visual Studio 소스 편집기 "버그" 인것 같음.. 이유는 위와 같이 어떨땐 경고표시되고 어떨땐 또 정상으로 잘 확인되고... 패턴을 모르겠음..

다른 문서를 찾아보면, mscorlib.dll 로딩 문제라고 하는데 그게 맞는건지도 모르겟고.. 원인 찾아볼려고 했는데... 다들 이런 저런 얘기가 있긴 한데,. 조금씩 다른 얘기들이라서 맞는건지도 모르겠고.. 그냥 이쯤에서 종료..;;





2021/11/02

c# netstat 예제

netstat.exe 에서 확인하던 정보를 C# 으로..  


        public void NetStat()
        {
            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] tcpConnInfoArray = ipProperties.GetActiveTcpConnections();

            foreach (TcpConnectionInformation tcpConnInfo in tcpConnInfoArray)
            {
                //TcpState tcpState = tcpConnInfo.State;
                //IPEndPoint localEndPoint = tcpConnInfo.LocalEndPoint;
                //IPEndPoint remotrEndPoint = tcpConnInfo.RemoteEndPoint;
            }
        }



더 세부적으로 깊게 접근하고 싶다면, Win32 라이브러리를 참조..;;

[DllImport("iphlpapi.dll",SetLastError=true)] 






2021/02/20

Failed to add reference to '???'. Please make sure that it is in the Global Assembly Cache. 오류

심각도 코드 설명 프로젝트 파일 비표시 오류(Suppression) 상태

오류 Failed to add reference to 'System.Linq'. Please make sure that it is in the Global Assembly Cache. 0


> 기본 참조 라이브러리 목록에 없기 때문에, 직접 해당 dll 파일 위치를 찾아서 지정...? 이게 진정 해결책? C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Linq\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Linq.dll   이렇게 직접 지정 ???

Nuget 패키지 시스템 버그 인가..?


That's a very confusing error message since the GAC isn't used to resolve references at compile time. Visual Studio should be looking under C:\Program Files(x86)\Reference Assemblies but that location shouldn't be referred to as the Global Assembly Cache.  이런것?


C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\Facades 에는 System.Linq.dll 이 있는데 여기 폴더는 참조 안함.?




 



'GetPathsOfAllDirectoriesAbove() 식을 계산할 수 없습니다' 오류

기존 Webapp 에 컴파일 버전이 2.x 인데, Nuget 에서 3.8 로 업그레이드 실행. 그런데 오류나 가서 완료되지 않았고,..  다른 MS 기본 라이브러리 업데이트도 안됨..  다시 VS 실행하니 참조 라이브러리 모두 오류..


◯◯◯◯.csproj : error  : ""◯◯◯◯.cs".GetPathsOfAllDirectoriesAbove()" 식을 계산할 수 없습니다. 'System.String.GetPathsOfAllDirectoriesAbove' 메서드를 찾을 수 없습니다. ◯◯◯◯\packages\Microsoft.Net.Compilers.3.8.0\tools\Microsoft.Managed.Core.targets


일단, VS ◯◯◯◯.csproj 프로젝트 설정 파일에서, Microsoft.Net.Compilers.3.8.0 항목을 수동 제거하고, 프로젝트 다시 로딩... 일단 경고 에러는 안뜸...

  <Import Project="..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props')" />

  <Import Project="..\packages\Microsoft.Net.Compilers.2.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.2.3.2\build\Microsoft.Net.Compilers.props')" />


   <ErrorText>이 프로젝트는 이 컴퓨터에 없는 NuGet 패키지를 참조합니다. 해당 패키지를 다운로드하려면 NuGet 패키지 복원을 사용하십시오. 자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=322105를 참조하십시오. 누락된 파일은 {0}입니다.</ErrorText>

    </PropertyGroup>

    <Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.2.3.2\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.2.3.2\build\Microsoft.Net.Compilers.props'))" />

    <Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.2.10.0\build\Microsoft.Net.Compilers.props'))" />

  </Target>


닷넷 4.6.1 프로젝트에서는 컴파일 3.8 버전이 지원안되나..? (종속성에는 버전 제한이 없음)  오류 대상 .cs 파일에 위 에러 구문 연관 클래스를 사용한게 없는데.... 어떤글에선 VS.NET 2019 버전에서만 3.x 지원된다고.. (현재 VS2015, 닷넷 4.6.1 기반)

https://www.nuget.org/packages/Microsoft.Net.Compilers/2.10.0  내용을 보면, 닷넷 4.6.1 에서는 최대 2.1 버전 까지 이고, 3.x 버전은 4.7.2+ 필요하다고 되어 있음.. > Nuget 패키지 관리자에서는 왜 업데이트 하라고 띄운것이며, 종속성 검사는 왜 안한건지...?


■ OS 에 설치된 닷넷 파일 버전은 4.8 까지 표시가되고 있으나, VS.NET 에는 4.6.1 까지만 나옴.. 별도 지원 파일을 설치하면 가능 : https://dotnet.microsoft.com/download/visual-studio-sdks?utm_source=getdotnetsdk&utm_medium=referral  에서 4.8 개발자팩 다운로드 설치.


어쨌든... 닷넷 4.x 에서는 컴파일러 3.8 버전은 설치 안되는듯.. 닷넷 5 부터 되는듯.. 정말.?  아무튼 컴파일러 버전 업데이트 포기.. 



2020/07/30

HttpClient 에서 비동기 호출시 System.Threading.Tasks.Task 오류

client.PostAsync().Result;

하나 이상의 오류가 발생했습니다., 위치: System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) 이

요청을 보내는 동안 오류가 발생했습니다.

[SocketException (0x2746): 현재 연결은 원격 호스트에 의해 강제로 끊겼습니다]
[IOException: 전송 연결 현재 연결은 원격 호스트에 의해 강제로 끊겼습니다에서 데이터를 읽을 수 없습니다.]
[WebException: 기본 연결이 닫혔습니다. 보내기에서 예기치 않은 오류가 발생했습니다.]



여러가지 원인중 하나 >

System.Net.ServicePointManager.SecurityProtocol
System.Net.ServicePointManager.ServerCertificateValidationCallback

구성이 되어 있지 않은 경우에 발생할 수 있음.
예를 들면, 서버에서 TLS 1.2 만 허용하는데 클라이언트에서 SSL 3.0 으로 접속할때.



2020/03/12

C# Win32 API 이용해서 GUI 핸들 확인하기


키움증권 API 이용해서 직접 개발한 닷넷 기반 주식 자동 매매 GUI 프로그램의, 동작 모니터링과 자동 제어가 필요해서 위해서 참조했던 코드.

실행된 GUI 프로그램에서,  특정 핸들 개체(예를 들면 버튼)가 존재하는지 확인 후, 해당 개체에 바인딩된 Property 값을 가져오거나 클릭/글자입력 같은 Action 을 주기 위해서 활용.

Process _trader = Process.GetProcessesByName("Trader").Single();
IntPtr _handle = _trader.MainWindowHandle;
IntPtr _apiConnet1 = Win32WindowApi.FindWindowEx(_handle, IntPtr.Zero, "WindowsForms10.STATIC.app.0.34f5582_r32_ad1", "OpenAPI");

GUI 컨트롤이 계층 구조인 경우 단계적으로 찾아 가면서 접근하거나, 해당 프로세스 핸들에 속한 모든 하위 핸들을 리스트로 뽑거나 검색하여 해당 컨트롤에 대해서 제어

foreach (IntPtr _child in Win32WindowApi.GetChildWindows(pHandle))
{
    HandleRef _ref = new HandleRef(this, _child);
    int capacity = Win32WindowApi.GetWindowTextLength(_ref) * 2;

    if (capacity > 0)
    {
        StringBuilder stringBuilder = new StringBuilder(capacity);
        Win32WindowApi.GetWindowText(_ref, stringBuilder, capacity);
        Console.WriteLine(">" + stringBuilder);
    }
}

특정 컨트롤(핸들)를 찾기 위해서는, MS Spy++ 같은 유틸을 이용해서 핸들 검색시 고유한 값으로 사용할만한 부분을 미리 찾아 놓으면 됨



    class Win32WindowApi
    {
        public const int WM_COMMAND = 0x0111;
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        public const int WM_LBUTTONDBLCLK = 0x0203;
        public const int WM_RBUTTONDBLCLK = 0x0206;
        public const uint WM_SETTEXT = 0x000C;  // 글자 설정
        public const int WM_CHAR = 0x0102;    // 타이핑
        public const int WM_SETFOCUS = 0x0007;
        public const int BM_CLICK = 0x00F5;
        public const int BN_CLICKED = 0;
        public const int MK_LBUTTON = 1;
 
        // Public Const WS_MINIMIZE = &H20000000
        // Public Const WS_MAXIMIZE = &H1000000
 
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
 
        public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
 
        [DllImport("User32.dll")]
        public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
 
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
 
        [DllImport("user32.Dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
 
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
 
        [DllImport("user32.dll")]
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
 
        [DllImport("user32.dll")]
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
 
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
 
        private static bool EnumWindow(IntPtr handle, IntPtr pointer)
        {
            GCHandle gch = GCHandle.FromIntPtr(pointer);
            List<IntPtr> list = gch.Target as List<IntPtr>;
            if (list == null)
                throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
            list.Add(handle);
            return true;
        }
 
        public static List<IntPtr> GetChildWindows(IntPtr parent)
        {
            List<IntPtr> result = new List<IntPtr>();
            GCHandle listHandle = GCHandle.Alloc(result);
            try
            {
                Win32Callback childProc = new Win32Callback(EnumWindow);
                EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return result;
        }
 
        public static string GetWinClass(IntPtr hwnd)
        {
            if (hwnd == IntPtr.Zero)
                return null;
            StringBuilder classname = new StringBuilder(100);
            IntPtr result = GetClassName(hwnd, classname, classname.Capacity);
            if (result != IntPtr.Zero)
                return classname.ToString();
            return null;
        }
 
        public static IEnumerable<IntPtr> EnumAllWindows(IntPtr hwnd, string childClassName)
        {
            List<IntPtr> children = GetChildWindows(hwnd);
            if (children == null)
            {
                yield break;
            }
 
            foreach (IntPtr child in children)
            {
                string _clsName = GetWinClass(child);
                //Console.WriteLine("find. {0:X}", _clsName);
 
                if (_clsName == childClassName)
                {
                    yield return child;
                }
                foreach (var childchild in EnumAllWindows(child, childClassName))
                {
                    //yield return childchild;
                }
            }
        }
    }


2019/08/13

C# Google API PhotosLibrary 예제

C# 예제가 거의 전무 하다 시피해서... 한참을  이것 저것 테스트 헤매다가 어케 어케....


using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using Google.Apis.PhotosLibrary.v1;
using Google.Apis.PhotosLibrary.v1.Data;


// 인증

UserCredential credential;

using (var stream = new FileStream("client_secret_oauth.json", FileMode.Open, FileAccess.Read))
{
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        new[] { PhotosLibraryService.Scope.Photoslibrary, PhotosLibraryService.Scope.DrivePhotosReadonly },
        "abcApp",   // 구분값
        CancellationToken.None,
        null).Result;
}

// 앨범 목록

PhotosLibraryService PhotosService = new PhotosLibraryService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
});

var albumsList = PhotosService.Albums.List().Execute();
foreach (var item in albumsList.Albums)
{
    Console.WriteLine("Album title: " + item.Title);
    Console.WriteLine("Album ID: " + item.Id);
}

// 사진 목록

SearchMediaItemsRequest x = new SearchMediaItemsRequest();
x.AlbumId = "AMhW9mr-M9UF3Tpo2";

var mitems = PhotosService.MediaItems.Search(x).Execute();  // 특정 앨범
foreach(var i in mitems.MediaItems)
{
    Console.WriteLine("Id : " +i.Id);
    Console.WriteLine("Filename : " + i.Filename);
    Console.WriteLine("BaseUrl : " + i.BaseUrl);
    Console.WriteLine("ProductUrl : " + i.ProductUrl);
}


2019/08/12

구글 API 인증 At least one client secrets (Installed or Web) should be set 에러


C#, Google.Apis.Blogger.v3 Auth

처리되지 않은 예외: System.InvalidOperationException: At least one client secrets (Installed or Web)
 should be set
   위치: Google.Apis.Auth.OAuth2.GoogleClientSecrets.get_Secrets()


json 인증값이, 구글 API 서비스 인증이 아닌 사용자 oAuth 인증으로 변경 적용해 볼 필요가 있음. (구글 여러 API 중에, 서비스 인증을 지원 안하는 API 인것 때문으로 보임)


가장 많이 본 글